home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / sed-2_00.lha / sed-2.00 / rx.c < prev    next >
C/C++ Source or Header  |  1993-07-15  |  213KB  |  8,352 lines

  1. /*    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this software; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17.             /* ``Too hard!''
  18.              *        -- anon.
  19.              */
  20.  
  21. /* NOTE!!!  AIX requires this to be the first thing in the file.
  22.    Do not put ANYTHING before it!  */
  23. #if !defined (__GNUC__) && defined (_AIX)
  24.  #pragma alloca
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #ifndef isgraph
  30. #define isgraph(c) (isprint (c) && !isspace (c))
  31. #endif
  32. #ifndef isblank
  33. #define isblank(c) ((c) == ' ' || (c) == '\t')
  34. #endif
  35.  
  36. #include <sys/types.h>
  37. #include <stdio.h>
  38. #include "rx.h"
  39.  
  40. #undef MAX
  41. #undef MIN
  42. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  43. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  44.  
  45. typedef char boolean;
  46. #define false 0
  47. #define true 1
  48.  
  49.  
  50.  
  51. /* This page is to the interesting subsystems and lower layers
  52.  * of rx.  Everything which doesn't have a public counterpart in 
  53.  * regex.c is declared here.
  54.  * 
  55.  * A useful (i hope) system is obtained by removing all or part of the regex.c
  56.  * reimplementation and making these all extern.  I think this package
  57.  * could be used to implement on-line lexers and parsers and who knows what 
  58.  * else.
  59.  */
  60. /* In the definitions, these functions are qualified by `RX_DECL' */
  61. #define RX_DECL static
  62.  
  63.  
  64. #ifdef __STDC__
  65.  
  66. static int rx_bitset_is_subset (int size, rx_Bitset a, rx_Bitset b);
  67. static void rx_bitset_null (int size, rx_Bitset b);
  68. static void rx_bitset_universe (int size, rx_Bitset b);
  69. static void rx_bitset_complement (int size, rx_Bitset b);
  70. static void rx_bitset_assign (int size, rx_Bitset a, rx_Bitset b);
  71. static void rx_bitset_union (int size, rx_Bitset a, rx_Bitset b);
  72. static void rx_bitset_intersection (int size,
  73.                     rx_Bitset a, rx_Bitset b);
  74. static void rx_bitset_difference (int size, rx_Bitset a, rx_Bitset b);
  75. static unsigned long rx_bitset_hash (int size, rx_Bitset b);
  76. static struct rx_hash_item * rx_hash_find (struct rx_hash * table,
  77.                        unsigned long hash,
  78.                        void * value,
  79.                        struct rx_hash_rules * rules);
  80. static struct rx_hash_item * rx_hash_store (struct rx_hash * table,
  81.                         unsigned long hash,
  82.                         void * value,
  83.                         struct rx_hash_rules * rules);
  84. static void rx_hash_free (struct rx_hash_item * it,
  85.               struct rx_hash_rules * rules);
  86. static rx_Bitset rx_cset (struct rx *rx);
  87. static rx_Bitset rx_copy_cset (struct rx *rx, rx_Bitset a);
  88. static void rx_free_cset (struct rx * rx, rx_Bitset c);
  89. static struct rexp_node * rexp_node (struct rx *rx,
  90.                      enum rexp_node_type type);
  91. static struct rexp_node * rx_mk_r_cset (struct rx * rx,
  92.                     rx_Bitset b);
  93. static struct rexp_node * rx_mk_r_concat (struct rx * rx,
  94.                       struct rexp_node * a,
  95.                       struct rexp_node * b);
  96. static struct rexp_node * rx_mk_r_alternate (struct rx * rx,
  97.                          struct rexp_node * a,
  98.                          struct rexp_node * b);
  99. static struct rexp_node * rx_mk_r_opt (struct rx * rx,
  100.                        struct rexp_node * a);
  101. static struct rexp_node * rx_mk_r_star (struct rx * rx,
  102.                     struct rexp_node * a);
  103. static struct rexp_node * rx_mk_r_2phase_star (struct rx * rx,
  104.                            struct rexp_node * a,
  105.                            struct rexp_node * b);
  106. static struct rexp_node * rx_mk_r_side_effect (struct rx * rx,
  107.                            rx_side_effect a);
  108. static struct rexp_node * rx_mk_r_data  (struct rx * rx,
  109.                      void * a);
  110. static void rx_free_rexp (struct rx * rx, struct rexp_node * node);
  111. static struct rexp_node * rx_copy_rexp (struct rx *rx,
  112.                      struct rexp_node *node);
  113. static struct rx_nfa_state * rx_nfa_state (struct rx *rx);
  114. static void rx_free_nfa_state (struct rx_nfa_state * n);
  115. static struct rx_nfa_state * rx_id_to_nfa_state (struct rx * rx,
  116.                          int id);
  117. static struct rx_nfa_edge * rx_nfa_edge (struct rx *rx,
  118.                      enum rx_nfa_etype type,
  119.                      struct rx_nfa_state *start,
  120.                      struct rx_nfa_state *dest);
  121. static void rx_free_nfa_edge (struct rx_nfa_edge * e);
  122. static void rx_free_nfa (struct rx *rx);
  123. static int rx_build_nfa (struct rx *rx,
  124.              struct rexp_node *rexp,
  125.              struct rx_nfa_state **start,
  126.              struct rx_nfa_state **end);
  127. static void rx_name_nfa_states (struct rx *rx);
  128. static int rx_eclose_nfa (struct rx *rx);
  129. static void rx_delete_epsilon_transitions (struct rx *rx);
  130. static int rx_compactify_nfa (struct rx *rx,
  131.                   void **mem, unsigned long *size);
  132. static struct rx_superset * rx_superstate_eclosure_union
  133.   (struct rx * rx, struct rx_superset *set, struct rx_nfa_state_set *ecl) ;
  134. static void rx_release_superset (struct rx *rx,
  135.                  struct rx_superset *set);
  136. static struct rx_superstate * rx_superstate (struct rx *rx,
  137.                          struct rx_superset *set);
  138. static struct rx_inx * rx_handle_cache_miss
  139.   (struct rx *rx, struct rx_superstate *super, unsigned char chr, void *data) ;
  140.  
  141. #else /* ndef __STDC__ */
  142. RX_DECL int rx_bitset_is_subset ();
  143. RX_DECL void rx_bitset_null ();
  144. RX_DECL void rx_bitset_universe ();
  145. RX_DECL void rx_bitset_complement ();
  146. RX_DECL void rx_bitset_assign ();
  147. RX_DECL void rx_bitset_union ();
  148. RX_DECL void rx_bitset_intersection ();
  149. RX_DECL void rx_bitset_difference ();
  150. RX_DECL unsigned long rx_bitset_hash ();
  151. RX_DECL struct rx_hash_item * rx_hash_find ();
  152. RX_DECL struct rx_hash_item * rx_hash_store ();
  153. RX_DECL void rx_hash_free ();
  154. RX_DECL rx_Bitset rx_cset ();
  155. RX_DECL rx_Bitset rx_copy_cset ();
  156. RX_DECL void rx_free_cset ();
  157. RX_DECL struct rexp_node * rexp_node ();
  158. RX_DECL struct rexp_node * rx_mk_r_cset ();
  159. RX_DECL struct rexp_node * rx_mk_r_concat ();
  160. RX_DECL struct rexp_node * rx_mk_r_alternate ();
  161. RX_DECL struct rexp_node * rx_mk_r_opt ();
  162. RX_DECL struct rexp_node * rx_mk_r_star ();
  163. RX_DECL struct rexp_node * rx_mk_r_2phase_star ();
  164. RX_DECL struct rexp_node * rx_mk_r_side_effect ();
  165. RX_DECL struct rexp_node * rx_mk_r_data  ();
  166. RX_DECL void rx_free_rexp ();
  167. RX_DECL struct rexp_node * rx_copy_rexp ();
  168. RX_DECL struct rx_nfa_state * rx_nfa_state ();
  169. RX_DECL void rx_free_nfa_state ();
  170. RX_DECL struct rx_nfa_state * rx_id_to_nfa_state ();
  171. RX_DECL struct rx_nfa_edge * rx_nfa_edge ();
  172. RX_DECL void rx_free_nfa_edge ();
  173. RX_DECL void rx_free_nfa ();
  174. RX_DECL int rx_build_nfa ();
  175. RX_DECL void rx_name_nfa_states ();
  176. RX_DECL int rx_eclose_nfa ();
  177. RX_DECL void rx_delete_epsilon_transitions ();
  178. RX_DECL int rx_compactify_nfa ();
  179. RX_DECL struct rx_superset * rx_superstate_eclosure_union ();
  180. RX_DECL void rx_release_superset ();
  181. RX_DECL struct rx_superstate * rx_superstate ();
  182. RX_DECL struct rx_inx * rx_handle_cache_miss ();
  183.   
  184. #endif /* ndef __STDC__ */
  185.  
  186.  
  187.  
  188. /* Emacs already defines alloca, sometimes.  */
  189. #ifndef alloca
  190.  
  191. /* Make alloca work the best possible way.  */
  192. #ifdef __GNUC__
  193. #define alloca __builtin_alloca
  194. #else /* not __GNUC__ */
  195. #if HAVE_ALLOCA_H
  196. #include <alloca.h>
  197. #else /* not __GNUC__ or HAVE_ALLOCA_H */
  198. #ifndef _AIX /* Already did AIX, up at the top.  */
  199. char *alloca ();
  200. #endif /* not _AIX */
  201. #endif /* not HAVE_ALLOCA_H */ 
  202. #endif /* not __GNUC__ */
  203.  
  204. #endif /* not alloca */
  205.  
  206.  
  207. /* Should we use malloc or alloca?  If REGEX_MALLOC is not defined, we
  208.    use `alloca' instead of `malloc' for the backtracking stack.
  209.  
  210.    Emacs will die miserably if we don't do this.
  211.   */
  212.  
  213. #ifdef REGEX_MALLOC
  214.  
  215. #define REGEX_ALLOCATE malloc
  216.  
  217. #else /* not REGEX_MALLOC  */
  218.  
  219. #define REGEX_ALLOCATE alloca
  220.  
  221. #endif /* not REGEX_MALLOC */
  222.  
  223.  
  224.  
  225.  
  226. /* Memory management and stuff for emacs. */
  227.  
  228. #define BYTEWIDTH 8 /* In bits.  */
  229.  
  230. /* (Re)Allocate N items of type T using malloc.  */
  231. #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
  232. #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  233.  
  234. #define remalloc(M, S) (M ? realloc (M, S) : malloc (S))
  235.  
  236. #ifdef emacs
  237. /* The `emacs' switch turns on certain matching commands
  238.  * that make sense only in Emacs. 
  239.  */
  240.  
  241. #include "config.h"
  242. #include "lisp.h"
  243. #include "buffer.h"
  244. #include "syntax.h"
  245.  
  246. /* Emacs uses `NULL' as a predicate.  */
  247. #undef NULL
  248. #else  /* not emacs */
  249.  
  250. /* Setting RX_MEMDBUG is useful if you have dbmalloc.  Maybe with similar
  251.  * packages too.
  252.  */
  253. #ifdef RX_MEMDBUG
  254. #include <malloc.h>
  255. #else /* not RX_RX_MEMDBUG */
  256.  
  257. /* We used to test for `BSTRING' here, but only GCC and Emacs define
  258.  * `BSTRING', as far as I know, and neither of them use this code.  
  259.  */
  260. #if HAVE_STRING_H || STDC_HEADERS
  261. #include <string.h>
  262. #ifndef bcmp
  263. #define bcmp(s1, s2, n)    memcmp ((s1), (s2), (n))
  264. #endif
  265. #ifndef bcopy
  266. #define bcopy(s, d, n)    memcpy ((d), (s), (n))
  267. #endif
  268. #ifndef bzero
  269. #define bzero(s, n)    memset ((s), 0, (n))
  270. #endif
  271. #else
  272. #include <strings.h>
  273. #endif
  274.  
  275. #ifdef STDC_HEADERS
  276. #include <stdlib.h>
  277. #else /* not STDC_HEADERS */
  278.  
  279. char *malloc ();
  280. char *realloc ();
  281. #endif /* not STDC_HEADERS */
  282.  
  283. #endif /* not RX_RX_MEMDBUG */
  284.  
  285.  
  286.  
  287. /* Define the syntax basics for \<, \>, etc.
  288.  * This must be nonzero for the wordchar and notwordchar pattern
  289.  * commands in re_match_2.
  290.  */
  291. #ifndef Sword 
  292. #define Sword 1
  293. #endif
  294.  
  295. #ifdef SYNTAX_TABLE
  296. extern char *re_syntax_table;
  297. #else /* not SYNTAX_TABLE */
  298.  
  299. /* How many characters in the character set.  */
  300. #define CHAR_SET_SIZE (1 << BYTEWIDTH)
  301. static char re_syntax_table[CHAR_SET_SIZE];
  302.  
  303. #ifdef __STDC__
  304. static void
  305. init_syntax_once (void)
  306. #else
  307. static void
  308. init_syntax_once ()
  309. #endif
  310. {
  311.    register int c;
  312.    static int done = 0;
  313.  
  314.    if (done)
  315.      return;
  316.  
  317.    bzero (re_syntax_table, sizeof re_syntax_table);
  318.  
  319.    for (c = 'a'; c <= 'z'; c++)
  320.      re_syntax_table[c] = Sword;
  321.  
  322.    for (c = 'A'; c <= 'Z'; c++)
  323.      re_syntax_table[c] = Sword;
  324.  
  325.    for (c = '0'; c <= '9'; c++)
  326.      re_syntax_table[c] = Sword;
  327.  
  328.    re_syntax_table['_'] = Sword;
  329.  
  330.    done = 1;
  331. }
  332. #endif /* not SYNTAX_TABLE */
  333.  
  334. #define SYNTAX(c) re_syntax_table[c]
  335.  
  336. #endif /* not emacs */
  337.  
  338.  
  339. /* Compile with `-DRX_DEBUG' and use the following flags.
  340.  *
  341.  * Debugging flags:
  342.  *       rx_debug - print information as a regexp is compiled
  343.  *     rx_debug_trace - print information as a regexp is executed
  344.  */
  345.  
  346. #ifdef RX_DEBUG
  347.  
  348. int rx_debug_compile = 0;
  349. int rx_debug_trace = 0;
  350. static struct re_pattern_buffer * dbug_rxb = 0;
  351.  
  352. #ifdef __STDC__
  353. typedef void (*side_effect_printer) (struct rx *, void *, FILE *);
  354. #else
  355. typedef void (*side_effect_printer) ();
  356. #endif
  357.  
  358. #ifdef __STDC__
  359. static void
  360. print_cset (struct rx *rx, rx_Bitset cset, FILE * fp)
  361. #else
  362. static void
  363. print_cset (rx, cset, fp)
  364.      struct rx *rx;
  365.      rx_Bitset cset;
  366.      FILE * fp;
  367. #endif
  368. {
  369.   int x;
  370.   fputc ('[', fp);
  371.   for (x = 0; x < rx->local_cset_size; ++x)
  372.     if (isprint(x) && RX_bitset_member (cset, x))
  373.       fputc (x, fp);
  374.   fputc (']', fp);
  375. }
  376.  
  377.  
  378. #ifdef __STDC__
  379. static void
  380. print_rexp (struct rx *rx,
  381.         struct rexp_node *node, int depth,
  382.         side_effect_printer seprint, FILE * fp)
  383. #else
  384. static void
  385. print_rexp (rx, node, depth, seprint, fp)
  386.      struct rx *rx;
  387.      struct rexp_node *node;
  388.      int depth;
  389.      side_effect_printer seprint;
  390.      FILE * fp;
  391. #endif
  392. {
  393.   if (!node)
  394.     return;
  395.   else
  396.     {
  397.       switch (node->type)
  398.     {
  399.     case r_cset:
  400.       {
  401.         fprintf (fp, "%*s", depth, "");
  402.         print_cset (rx, node->params.cset, fp);
  403.         fputc ('\n', fp);
  404.         break;
  405.       }
  406.  
  407.      case r_opt:
  408.     case r_star:
  409.       fprintf (fp, "%*s%s\n", depth, "",
  410.            node->type == r_opt ? "opt" : "star");
  411.       print_rexp (rx, node->params.pair.left, depth + 3, seprint, fp);
  412.       break;
  413.  
  414.     case r_2phase_star:
  415.       fprintf (fp, "%*s2phase star\n", depth, "");
  416.       print_rexp (rx, node->params.pair.right, depth + 3, seprint, fp);
  417.       print_rexp (rx, node->params.pair.left, depth + 3, seprint, fp);
  418.       break;
  419.  
  420.  
  421.     case r_alternate:
  422.     case r_concat:
  423.       fprintf (fp, "%*s%s\n", depth, "",
  424.            node->type == r_alternate ? "alt" : "concat");
  425.       print_rexp (rx, node->params.pair.left, depth + 3, seprint, fp);
  426.       print_rexp (rx, node->params.pair.right, depth + 3, seprint, fp);
  427.       break;
  428.     case r_side_effect:
  429.       fprintf (fp, "%*sSide effect: ", depth, "");
  430.       seprint (rx, node->params.side_effect, fp);
  431.       fputc ('\n', fp);
  432.     }
  433.     }
  434. }
  435.  
  436.  
  437. #ifdef __STDC__
  438. static void
  439. print_nfa (struct rx * rx,
  440.        struct rx_nfa_state * n,
  441.        side_effect_printer seprint, FILE * fp)
  442. #else
  443. static void
  444. print_nfa (rx, n, seprint, fp)
  445.      struct rx * rx;
  446.      struct rx_nfa_state * n;
  447.      side_effect_printer seprint;
  448.      FILE * fp;
  449. #endif
  450. {
  451.   while (n)
  452.     {
  453.       struct rx_nfa_edge *e = n->edges;
  454.       struct rx_possible_future *ec = n->futures;
  455.       fprintf (fp, "node %d %s\n", n->id,
  456.            n->is_final ? "final" : (n->is_start ? "start" : ""));
  457.       while (e)
  458.     {
  459.       fprintf (fp, "   edge to %d, ", e->dest->id);
  460.       switch (e->type)
  461.         {
  462.         case ne_epsilon:
  463.           fprintf (fp, "epsilon\n");
  464.           break;
  465.         case ne_side_effect:
  466.           fprintf (fp, "side effect ");
  467.           seprint (rx, e->params.side_effect, fp);
  468.           fputc ('\n', fp);
  469.           break;
  470.         case ne_cset:
  471.           fprintf (fp, "cset ");
  472.           print_cset (rx, e->params.cset, fp);
  473.           fputc ('\n', fp);
  474.           break;
  475.         }
  476.       e = e->next;
  477.     }
  478.  
  479.       while (ec)
  480.     {
  481.       int x;
  482.       struct rx_nfa_state_set * s;
  483.       struct rx_se_list * l;
  484.       fprintf (fp, "   eclosure to {");
  485.       for (s = ec->destset; s; s = s->cdr)
  486.         fprintf (fp, "%d ", s->car->id);
  487.       fprintf (fp, "} (");
  488.       for (l = ec->effects; l; l = l->cdr)
  489.         {
  490.           seprint (rx, l->car, fp);
  491.           fputc (' ', fp);
  492.         }
  493.       fprintf (fp, ")\n");
  494.       ec = ec->next;
  495.     }
  496.       n = n->next;
  497.     }
  498. }
  499.  
  500. static char * efnames [] =
  501. {
  502.   "bogon",
  503.   "re_se_try",
  504.   "re_se_pushback",
  505.   "re_se_push0",
  506.   "re_se_pushpos",
  507.   "re_se_chkpos",
  508.   "re_se_poppos",
  509.   "re_se_at_dot",
  510.   "re_se_syntax",
  511.   "re_se_not_syntax",
  512.   "re_se_begbuf",
  513.   "re_se_hat",
  514.   "re_se_wordbeg",
  515.   "re_se_wordbound",
  516.   "re_se_notwordbound",
  517.   "re_se_wordend",
  518.   "re_se_endbuf",
  519.   "re_se_dollar",
  520.   "re_se_fail",
  521.   "re_se_win"
  522. };
  523.  
  524. static char * efnames2[] =
  525. {
  526.   "re_se_lparen",
  527.   "re_se_rparen",
  528.   "re_se_backref",
  529.   "re_se_iter",
  530.   "re_se_end_iter"
  531. };
  532.  
  533. static char * inx_names[] = 
  534. {
  535.   "rx_backtrack_point",
  536.   "rx_do_side_effects",
  537.   "rx_cache_miss",
  538.   "rx_next_char",
  539.   "rx_backtrack",
  540.   "rx_error_inx",
  541.   "rx_num_instructions"
  542. };
  543.  
  544.  
  545. #ifdef __STDC__
  546. static void
  547. re_seprint (struct rx * rx, void * effect, FILE * fp)
  548. #else
  549. static void
  550. re_seprint (rx, effect, fp)
  551.      struct rx * rx;
  552.      void * effect;
  553.      FILE * fp;
  554. #endif
  555. {
  556.   if ((int)effect < 0)
  557.     fputs (efnames[-(int)effect], fp);
  558.   else if (dbug_rxb)
  559.     {
  560.       struct re_se_params * p = &dbug_rxb->se_params[(int)effect];
  561.       fprintf (fp, "%s(%d,%d)", efnames2[p->se], p->op1, p->op2);
  562.     }
  563.   else
  564.     fprintf (fp, "[complex op # %d]", (int)effect);
  565. }
  566.  
  567.  
  568. /* These are for so the regex.c regression tests will compile. */
  569. void
  570. print_compiled_pattern (rxb)
  571.      struct re_pattern_buffer * rxb;
  572. {
  573. }
  574.  
  575. void
  576. print_fastmap (fm)
  577.      char * fm;
  578. {
  579. }
  580.  
  581.  
  582.  
  583. #endif /* RX_DEBUG */
  584.  
  585.  
  586.  
  587. /* This page: Bitsets.  Completely unintersting. */
  588.  
  589. #if 0
  590. #ifdef __STDC__
  591. RX_DECL int
  592. rx_bitset_is_equal (int size, rx_Bitset a, rx_Bitset b)
  593. #else
  594. RX_DECL int
  595. rx_bitset_is_equal (size, a, b)
  596.      int size;
  597.      rx_Bitset a;
  598.      rx_Bitset b;
  599. #endif
  600. {
  601.   int x;
  602.   RX_subset s = b[0];
  603.   b[0] = ~a[0];
  604.  
  605.   for (x = rx_bitset_numb_subsets(size) - 1; a[x] == b[x]; --x)
  606.     ;
  607.  
  608.   b[0] = s;
  609.   return !x && s == a[0];
  610. }
  611. #endif
  612.  
  613. #ifdef __STDC__
  614. RX_DECL int
  615. rx_bitset_is_subset (int size, rx_Bitset a, rx_Bitset b)
  616. #else
  617. RX_DECL int
  618. rx_bitset_is_subset (size, a, b)
  619.      int size;
  620.      rx_Bitset a;
  621.      rx_Bitset b;
  622. #endif
  623. {
  624.   int x = rx_bitset_numb_subsets(size) - 1;
  625.   while (x-- && (a[x] & b[x]) == a[x]);
  626.   return x == -1;
  627. }
  628.  
  629.  
  630. #if 0
  631. #ifdef __STDC__
  632. RX_DECL int
  633. rx_bitset_empty (int size, rx_Bitset set)
  634. #else
  635. RX_DECL int
  636. rx_bitset_empty (size, set)
  637.      int size;
  638.      rx_Bitset set;
  639. #endif
  640. {
  641.   int x;
  642.   RX_subset s = set[0];
  643.   set[0] = 1;
  644.   for (x = rx_bitset_numb_subsets(size) - 1; !set[x]; --x)
  645.     ;
  646.   set[0] = s;
  647.   return !s;
  648. }
  649. #endif
  650.  
  651. #ifdef __STDC__
  652. RX_DECL void
  653. rx_bitset_null (int size, rx_Bitset b)
  654. #else
  655. RX_DECL void
  656. rx_bitset_null (size, b)
  657.      int size;
  658.      rx_Bitset b;
  659. #endif
  660. {
  661.   bzero (b, rx_sizeof_bitset(size));
  662. }
  663.  
  664.  
  665. #ifdef __STDC__
  666. RX_DECL void
  667. rx_bitset_universe (int size, rx_Bitset b)
  668. #else
  669. RX_DECL void
  670. rx_bitset_universe (size, b)
  671.      int size;
  672.      rx_Bitset b;
  673. #endif
  674. {
  675.   int x = rx_bitset_numb_subsets (size);
  676.   while (x--)
  677.     *b++ = ~(RX_subset)0;
  678. }
  679.  
  680.  
  681. #ifdef __STDC__
  682. RX_DECL void
  683. rx_bitset_complement (int size, rx_Bitset b)
  684. #else
  685. RX_DECL void
  686. rx_bitset_complement (size, b)
  687.      int size;
  688.      rx_Bitset b;
  689. #endif
  690. {
  691.   int x = rx_bitset_numb_subsets (size);
  692.   while (x--)
  693.     {
  694.       *b = ~*b;
  695.       ++b;
  696.     }
  697. }
  698.  
  699.  
  700. #ifdef __STDC__
  701. RX_DECL void
  702. rx_bitset_assign (int size, rx_Bitset a, rx_Bitset b)
  703. #else
  704. RX_DECL void
  705. rx_bitset_assign (size, a, b)
  706.      int size;
  707.      rx_Bitset a;
  708.      rx_Bitset b;
  709. #endif
  710. {
  711.   int x;
  712.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  713.     a[x] = b[x];
  714. }
  715.  
  716.  
  717. #ifdef __STDC__
  718. RX_DECL void
  719. rx_bitset_union (int size, rx_Bitset a, rx_Bitset b)
  720. #else
  721. RX_DECL void
  722. rx_bitset_union (size, a, b)
  723.      int size;
  724.      rx_Bitset a;
  725.      rx_Bitset b;
  726. #endif
  727. {
  728.   int x;
  729.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  730.     a[x] |= b[x];
  731. }
  732.  
  733.  
  734. #ifdef __STDC__
  735. RX_DECL void
  736. rx_bitset_intersection (int size,
  737.             rx_Bitset a, rx_Bitset b)
  738. #else
  739. RX_DECL void
  740. rx_bitset_intersection (size, a, b)
  741.      int size;
  742.      rx_Bitset a;
  743.      rx_Bitset b;
  744. #endif
  745. {
  746.   int x;
  747.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  748.     a[x] &= b[x];
  749. }
  750.  
  751.  
  752. #ifdef __STDC__
  753. RX_DECL void
  754. rx_bitset_difference (int size, rx_Bitset a, rx_Bitset b)
  755. #else
  756. RX_DECL void
  757. rx_bitset_difference (size, a, b)
  758.      int size;
  759.      rx_Bitset a;
  760.      rx_Bitset b;
  761. #endif
  762. {
  763.   int x;
  764.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  765.     a[x] &=  ~ b[x];
  766. }
  767.  
  768.  
  769. #if 0
  770. #ifdef __STDC__
  771. RX_DECL void
  772. rx_bitset_revdifference (int size,
  773.              rx_Bitset a, rx_Bitset b)
  774. #else
  775. RX_DECL void
  776. rx_bitset_revdifference (size, a, b)
  777.      int size;
  778.      rx_Bitset a;
  779.      rx_Bitset b;
  780. #endif
  781. {
  782.   int x;
  783.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  784.     a[x] = ~a[x] & b[x];
  785. }
  786.  
  787. #ifdef __STDC__
  788. RX_DECL void
  789. rx_bitset_xor (int size, rx_Bitset a, rx_Bitset b)
  790. #else
  791. RX_DECL void
  792. rx_bitset_xor (size, a, b)
  793.      int size;
  794.      rx_Bitset a;
  795.      rx_Bitset b;
  796. #endif
  797. {
  798.   int x;
  799.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  800.     a[x] ^= b[x];
  801. }
  802. #endif
  803.  
  804.  
  805. #ifdef __STDC__
  806. RX_DECL unsigned long
  807. rx_bitset_hash (int size, rx_Bitset b)
  808. #else
  809. RX_DECL unsigned long
  810. rx_bitset_hash (size, b)
  811.      int size;
  812.      rx_Bitset b;
  813. #endif
  814. {
  815.   int x;
  816.   unsigned long hash = (unsigned long)rx_bitset_hash;
  817.  
  818.   for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x)
  819.     hash ^= rx_bitset_subset_val(b, x);
  820.  
  821.   return hash;
  822. }
  823.  
  824.  
  825. RX_DECL RX_subset rx_subset_singletons [RX_subset_bits] = 
  826. {
  827.   0x1,
  828.   0x2,
  829.   0x4,
  830.   0x8,
  831.   0x10,
  832.   0x20,
  833.   0x40,
  834.   0x80,
  835.   0x100,
  836.   0x200,
  837.   0x400,
  838.   0x800,
  839.   0x1000,
  840.   0x2000,
  841.   0x4000,
  842.   0x8000,
  843.   0x10000,
  844.   0x20000,
  845.   0x40000,
  846.   0x80000,
  847.   0x100000,
  848.   0x200000,
  849.   0x400000,
  850.   0x800000,
  851.   0x1000000,
  852.   0x2000000,
  853.   0x4000000,
  854.   0x8000000,
  855.   0x10000000,
  856.   0x20000000,
  857.   0x40000000,
  858.   0x80000000
  859. };
  860.  
  861.  
  862.  
  863.  
  864. /* This page: small object pools.
  865.  */
  866.  
  867. struct freelist
  868. {
  869.   struct freelist * next;
  870. };
  871.  
  872. struct chunked_pool
  873. {
  874.   int size;
  875.   struct freelist * freelist;
  876.   char * chunk;
  877.   int num_left;
  878. #if RX_DEBUG
  879.   int leakiness;
  880. #endif
  881. };
  882.  
  883. #define DEF_POOL(NAME,TYPE) \
  884.   struct chunked_pool NAME = { sizeof(TYPE), 0, 0, 0 }
  885.  
  886.  
  887. #ifdef __STDC__
  888. static char *
  889. chunk_malloc (struct chunked_pool * pool)
  890. #else
  891. static char *
  892. chunk_malloc (pool)
  893.      struct chunked_pool * pool;
  894. #endif
  895. {
  896.   struct freelist * it;
  897.   if (pool->freelist)
  898.     {
  899.       it = pool->freelist;
  900.       pool->freelist = it->next;
  901.     }
  902.   else
  903.     {
  904.       if (!pool->num_left)
  905.     {
  906.       pool->chunk = (char *)malloc (pool->size * 128);
  907.       if (!pool->chunk)
  908.         return 0;
  909.       pool->num_left = 128;
  910.     }
  911.       it = (struct freelist *)pool->chunk;
  912.       pool->chunk += pool->size;
  913.       --pool->num_left;
  914.     }
  915. #if RX_DEBUG
  916.   if (it)
  917.     ++pool->leakiness;
  918. #endif
  919.   return (char *)it;
  920. }
  921.  
  922.  
  923. #ifdef __STDC__
  924. static void
  925. chunk_free (struct chunked_pool * pool, char * mem)
  926. #else
  927. static void
  928. chunk_free (pool, mem)
  929.      struct chunked_pool * pool;
  930.      char * mem;
  931. #endif
  932. {
  933.   struct freelist * it = (struct freelist *)mem;
  934.   it->next = pool->freelist;
  935.   pool->freelist = it;
  936. #if RX_DEBUG
  937.   --pool->leakiness;
  938. #endif
  939. }
  940.  
  941. /* This is for pools of variable size objects where there are only a few
  942.  * sizes and where we tend to burst on one size at a time.
  943.  */
  944.  
  945. struct linked_chunk
  946. {
  947.   struct chunked_pool pool;
  948.   struct linked_chunk * next;
  949. };
  950.  
  951. struct chunk_group
  952. {
  953.   struct linked_chunk * list;
  954.   struct linked_chunk * current;
  955. };
  956.  
  957.  
  958. #ifdef __STDC__
  959. static int
  960. cg_find_pool (struct chunk_group * group, int size)
  961. #else
  962. static int
  963. cg_find_pool (group, size)
  964.      struct chunk_group * group;
  965.      int size;
  966. #endif
  967. {
  968.   if (!(group->current && (group->current->pool.size == size)))
  969.     {
  970.       struct linked_chunk * lc = group->list;
  971.       while (lc)
  972.     if (lc->pool.size == size)
  973.       {
  974.         group->current = lc;
  975.         return 1;
  976.       }
  977.       else
  978.     lc = lc->next;
  979.       lc = (struct linked_chunk *)malloc (sizeof (*lc));
  980.       if (!lc)
  981.     return 0;
  982.       bzero (lc, sizeof (*lc));
  983.       lc->pool.size = size;
  984.       lc->next = group->list;
  985.       group->list = lc;
  986.       group->current = lc;
  987.     }
  988.   return 1;
  989. }
  990.  
  991.  
  992. #ifdef __STDC__
  993. static char *
  994. cg_malloc (struct chunk_group * group, int size)
  995. #else
  996. static char *
  997. cg_malloc (group, size)
  998.      struct chunk_group * group;
  999.      int size;
  1000. #endif
  1001. {
  1002.   return (cg_find_pool (group, size)
  1003.       ?  chunk_malloc (&group->current->pool)
  1004.       : 0);
  1005. }
  1006.  
  1007.  
  1008. #ifdef __STDC__
  1009. static void
  1010. cg_free (struct chunk_group * group, int size, char * mem)
  1011. #else
  1012. static void
  1013. cg_free (group, size, mem)
  1014.      struct chunk_group * group;
  1015.      int size;
  1016.      char * mem;
  1017. #endif
  1018. {
  1019.   if (cg_find_pool (group, size))
  1020.     chunk_free (&group->current->pool, mem);
  1021. }
  1022.  
  1023.  
  1024.  
  1025. static unsigned long rx_hash_masks[4] =
  1026. {
  1027.   0x12488421,
  1028.   0x96699669,
  1029.   0xbe7dd7eb,
  1030.   0xffffffff
  1031. };
  1032.  
  1033.  
  1034. /* Hash tables */
  1035. #ifdef __STDC__
  1036. RX_DECL struct rx_hash_item * 
  1037. rx_hash_find (struct rx_hash * table,
  1038.           unsigned long hash,
  1039.           void * value,
  1040.           struct rx_hash_rules * rules)
  1041. #else
  1042. RX_DECL struct rx_hash_item * 
  1043. rx_hash_find (table, hash, value, rules)
  1044.      struct rx_hash * table;
  1045.      unsigned long hash;
  1046.      void * value;
  1047.      struct rx_hash_rules * rules;
  1048. #endif
  1049. {
  1050.   rx_hash_eq eq = rules->eq;
  1051.   int maskc = 0;
  1052.   int mask = rx_hash_masks [0];
  1053.   int bucket = (hash & mask) % 13;
  1054.  
  1055.   while (table->children [bucket])
  1056.     {
  1057.       table = table->children [bucket];
  1058.       ++maskc;
  1059.       mask = rx_hash_masks[maskc];
  1060.       bucket = (hash & mask) % 13;
  1061.     }
  1062.  
  1063.   {
  1064.     struct rx_hash_item * it = table->buckets[bucket];
  1065.     while (it)
  1066.       if (eq (it->data, value))
  1067.     return it;
  1068.       else
  1069.     it = it->next_same_hash;
  1070.   }
  1071.  
  1072.   return 0;
  1073. }
  1074.  
  1075. #ifdef __STDC__
  1076. RX_DECL struct rx_hash_item *
  1077. rx_hash_store (struct rx_hash * table,
  1078.            unsigned long hash,
  1079.            void * value,
  1080.            struct rx_hash_rules * rules)
  1081. #else
  1082. RX_DECL struct rx_hash_item *
  1083. rx_hash_store (table, hash, value, rules)
  1084.      struct rx_hash * table;
  1085.      unsigned long hash;
  1086.      void * value;
  1087.      struct rx_hash_rules * rules;
  1088. #endif
  1089. {
  1090.   rx_hash_eq eq = rules->eq;
  1091.   int maskc = 0;
  1092.   int mask = rx_hash_masks[0];
  1093.   int bucket = (hash & mask) % 13;
  1094.   int depth = 0;
  1095.   
  1096.   while (table->children [bucket])
  1097.     {
  1098.       table = table->children [bucket];
  1099.       ++maskc;
  1100.       mask = rx_hash_masks[maskc];
  1101.       bucket = (hash & mask) % 13;
  1102.       ++depth;
  1103.     }
  1104.   
  1105.   {
  1106.     struct rx_hash_item * it = table->buckets[bucket];
  1107.     while (it)
  1108.       if (eq (it->data, value))
  1109.     return it;
  1110.       else
  1111.     it = it->next_same_hash;
  1112.   }
  1113.   
  1114.   {
  1115.     if (   (depth < 3)
  1116.     && (table->bucket_size [bucket] >= 4))
  1117.       {
  1118.     struct rx_hash * newtab = ((struct rx_hash *)
  1119.                    rules->hash_alloc (rules));
  1120.     if (!newtab)
  1121.       goto add_to_bucket;
  1122.     bzero (newtab, sizeof (*newtab));
  1123.     newtab->parent = table;
  1124.     {
  1125.       struct rx_hash_item * them = table->buckets[bucket];
  1126.       unsigned long newmask = rx_hash_masks[maskc + 1];
  1127.       while (them)
  1128.         {
  1129.           struct rx_hash_item * save = them->next_same_hash;
  1130.           int new_buck = (them->hash & newmask) % 13;
  1131.           them->next_same_hash = newtab->buckets[new_buck];
  1132.           newtab->buckets[new_buck] = them;
  1133.           them->table = newtab;
  1134.           them = save;
  1135.           ++newtab->bucket_size[new_buck];
  1136.           ++newtab->refs;
  1137.         }
  1138.       table->refs = (table->refs - table->bucket_size[bucket] + 1);
  1139.       table->bucket_size[bucket] = 0;
  1140.       table->buckets[bucket] = 0;
  1141.       table->children[bucket] = newtab;
  1142.       table = newtab;
  1143.       bucket = (hash & newmask) % 13;
  1144.     }
  1145.       }
  1146.   }
  1147.  add_to_bucket:
  1148.   {
  1149.     struct rx_hash_item  * it = ((struct rx_hash_item *)
  1150.                  rules->hash_item_alloc (rules, value));
  1151.     if (!it)
  1152.       return 0;
  1153.     it->hash = hash;
  1154.     it->table = table;
  1155.     /* DATA and BINDING are to be set in hash_item_alloc */
  1156.     it->next_same_hash = table->buckets [bucket];
  1157.     table->buckets[bucket] = it;
  1158.     ++table->bucket_size [bucket];
  1159.     ++table->refs;
  1160.     return it;
  1161.   }
  1162. }
  1163.  
  1164. #ifdef __STDC__
  1165. RX_DECL void
  1166. rx_hash_free (struct rx_hash_item * it, struct rx_hash_rules * rules)
  1167. #else
  1168. RX_DECL void
  1169. rx_hash_free (it, rules)
  1170.      struct rx_hash_item * it;
  1171.      struct rx_hash_rules * rules;
  1172. #endif
  1173. {
  1174.   if (it)
  1175.     {
  1176.       struct rx_hash * table = it->table;
  1177.       unsigned long hash = it->hash;
  1178.       int depth = (table->parent
  1179.            ? (table->parent->parent
  1180.               ? (table->parent->parent->parent
  1181.              ? 3
  1182.              : 2)
  1183.               : 1)
  1184.            : 0);
  1185.       int bucket = (hash & rx_hash_masks [depth]) % 13;
  1186.       struct rx_hash_item ** pos = &table->buckets [bucket];
  1187.       
  1188.       while (*pos != it)
  1189.     pos = &(*pos)->next_same_hash;
  1190.       *pos = it->next_same_hash;
  1191.       rules->free_hash_item (it, rules);
  1192.       --table->bucket_size[bucket];
  1193.       --table->refs;
  1194.       while (!table->refs && depth)
  1195.     {
  1196.       struct rx_hash * save = table;
  1197.       table = table->parent;
  1198.       --depth;
  1199.       bucket = (hash & rx_hash_masks [depth]) % 13;
  1200.       --table->refs;
  1201.       table->children[bucket] = 0;
  1202.       rules->free_hash (save, rules);
  1203.     }
  1204.     }
  1205. }
  1206.  
  1207. #ifdef __STDC__
  1208. typedef void (*rx_hash_freefn) (struct rx_hash_item * it);
  1209. #else /* ndef __STDC__ */
  1210. typedef void (*rx_hash_freefn) ();
  1211. #endif /* ndef __STDC__ */
  1212.  
  1213. #ifdef __STDC__
  1214. RX_DECL void
  1215. rx_free_hash_table (struct rx_hash * tab, rx_hash_freefn freefn,
  1216.             struct rx_hash_rules * rules)
  1217. #else
  1218. RX_DECL void
  1219. rx_free_hash_table (tab, freefn, rules)
  1220.      struct rx_hash * tab;
  1221.      rx_hash_freefn freefn;
  1222.      struct rx_hash_rules * rules;
  1223. #endif
  1224. {
  1225.   int x;
  1226.  
  1227.   for (x = 0; x < 13; ++x)
  1228.     if (tab->children[x])
  1229.       {
  1230.     rx_free_hash_table (tab->children[x], freefn, rules);
  1231.     rules->free_hash (tab->children[x], rules);
  1232.       }
  1233.     else
  1234.       {
  1235.     struct rx_hash_item * them = tab->buckets[x];
  1236.     while (them)
  1237.       {
  1238.         struct rx_hash_item * that = them;
  1239.         them = that->next_same_hash;
  1240.         freefn (that);
  1241.         rules->free_hash_item (that, rules);
  1242.       }
  1243.       }
  1244. }
  1245.  
  1246.  
  1247.  
  1248. /* Utilities for manipulating bitset represntations of characters sets. */
  1249.  
  1250. static struct chunk_group cset_chunks = {0, 0};
  1251.  
  1252. #ifdef __STDC__
  1253. RX_DECL rx_Bitset
  1254. rx_cset (struct rx *rx)
  1255. #else
  1256. RX_DECL rx_Bitset
  1257. rx_cset (rx)
  1258.      struct rx *rx;
  1259. #endif
  1260. {
  1261.   rx_Bitset b = (rx_Bitset) cg_malloc (&cset_chunks,
  1262.                        rx_sizeof_bitset (rx->local_cset_size));
  1263.   if (b)
  1264.     rx_bitset_null (rx->local_cset_size, b);
  1265.   return b;
  1266. }
  1267.  
  1268.  
  1269. #ifdef __STDC__
  1270. RX_DECL rx_Bitset
  1271. rx_copy_cset (struct rx *rx, rx_Bitset a)
  1272. #else
  1273. RX_DECL rx_Bitset
  1274. rx_copy_cset (rx, a)
  1275.      struct rx *rx;
  1276.      rx_Bitset a;
  1277. #endif
  1278. {
  1279.   rx_Bitset cs = rx_cset (rx);
  1280.  
  1281.   if (cs)
  1282.     rx_bitset_union (rx->local_cset_size, cs, a);
  1283.  
  1284.   return cs;
  1285. }
  1286.  
  1287.  
  1288. #ifdef __STDC__
  1289. RX_DECL void
  1290. rx_free_cset (struct rx * rx, rx_Bitset c)
  1291. #else
  1292. RX_DECL void
  1293. rx_free_cset (rx, c)
  1294.      struct rx * rx;
  1295.      rx_Bitset c;
  1296. #endif
  1297. {
  1298.   if (c)
  1299.     cg_free (&cset_chunks, rx_sizeof_bitset (rx->local_cset_size), (char *)c);
  1300. }
  1301.  
  1302.  
  1303. /* Hash table memory allocation policy for the regexp compiler */
  1304.  
  1305. static DEF_POOL(hash_tabs, struct rx_hash);
  1306. static DEF_POOL(hash_items, struct rx_hash_item);
  1307.  
  1308. #ifdef __STDC__
  1309. struct rx_hash *
  1310. compiler_hash_alloc (struct rx_hash_rules * rules)
  1311. #else
  1312. struct rx_hash *
  1313. compiler_hash_alloc (rules)
  1314.      struct rx_hash_rules * rules;
  1315. #endif
  1316. {
  1317.   return (struct rx_hash *)chunk_malloc (&hash_tabs);
  1318. }
  1319.  
  1320. #ifdef __STDC__
  1321. struct rx_hash_item *
  1322. compiler_hash_item_alloc (struct rx_hash_rules * rules, void * value)
  1323. #else
  1324. struct rx_hash_item *
  1325. compiler_hash_item_alloc (rules, value)
  1326.      struct rx_hash_rules * rules;
  1327.      void * value;
  1328. #endif
  1329. {
  1330.   struct rx_hash_item * it = (struct rx_hash_item *)chunk_malloc (&hash_items);
  1331.   if (it)
  1332.     {
  1333.       it->data = value;
  1334.       it->binding = 0;
  1335.     }
  1336.   return it;
  1337. }
  1338.  
  1339. #ifdef __STDC__
  1340. void
  1341. compiler_free_hash (struct rx_hash * tab,
  1342.             struct rx_hash_rules * rules)
  1343. #else
  1344. void
  1345. compiler_free_hash (tab, rules)
  1346.      struct rx_hash * tab;
  1347.      struct rx_hash_rules * rules;
  1348. #endif
  1349. {
  1350.   chunk_free (&hash_tabs, (char *)tab);
  1351. }
  1352.  
  1353. #ifdef __STDC__
  1354. void
  1355. compiler_free_hash_item (struct rx_hash_item * item,
  1356.              struct rx_hash_rules * rules)
  1357. #else
  1358. void
  1359. compiler_free_hash_item (item, rules)
  1360.      struct rx_hash_item * item;
  1361.      struct rx_hash_rules * rules;
  1362. #endif
  1363. {
  1364.   chunk_free (&hash_items, (char *)item);
  1365. }
  1366.  
  1367.  
  1368. /* This page: REXP_NODE (expression tree) structures. */
  1369.  
  1370. static DEF_POOL (rexp_pool, struct rexp_node);
  1371.  
  1372. #ifdef __STDC__
  1373. RX_DECL struct rexp_node *
  1374. rexp_node (struct rx *rx,
  1375.        enum rexp_node_type type)
  1376. #else
  1377. RX_DECL struct rexp_node *
  1378. rexp_node (rx, type)
  1379.      struct rx *rx;
  1380.      enum rexp_node_type type;
  1381. #endif
  1382. {
  1383.   struct rexp_node *n;
  1384.  
  1385.   n = (struct rexp_node *)chunk_malloc (&rexp_pool);
  1386.   bzero (n, sizeof (*n));
  1387.   if (n)
  1388.     n->type = type;
  1389.   return n;
  1390. }
  1391.  
  1392.  
  1393. /* free_rexp_node assumes that the bitset passed to rx_mk_r_cset
  1394.  * can be freed using rx_free_cset.
  1395.  */
  1396. #ifdef __STDC__
  1397. RX_DECL struct rexp_node *
  1398. rx_mk_r_cset (struct rx * rx,
  1399.           rx_Bitset b)
  1400. #else
  1401. RX_DECL struct rexp_node *
  1402. rx_mk_r_cset (rx, b)
  1403.      struct rx * rx;
  1404.      rx_Bitset b;
  1405. #endif
  1406. {
  1407.   struct rexp_node * n = rexp_node (rx, r_cset);
  1408.   if (n)
  1409.     n->params.cset = b;
  1410.   return n;
  1411. }
  1412.  
  1413.  
  1414. #ifdef __STDC__
  1415. RX_DECL struct rexp_node *
  1416. rx_mk_r_concat (struct rx * rx,
  1417.         struct rexp_node * a,
  1418.         struct rexp_node * b)
  1419. #else
  1420. RX_DECL struct rexp_node *
  1421. rx_mk_r_concat (rx, a, b)
  1422.      struct rx * rx;
  1423.      struct rexp_node * a;
  1424.      struct rexp_node * b;
  1425. #endif
  1426. {
  1427.   struct rexp_node * n = rexp_node (rx, r_concat);
  1428.   if (n)
  1429.     {
  1430.       n->params.pair.left = a;
  1431.       n->params.pair.right = b;
  1432.     }
  1433.   return n;
  1434. }
  1435.  
  1436.  
  1437. #ifdef __STDC__
  1438. RX_DECL struct rexp_node *
  1439. rx_mk_r_alternate (struct rx * rx,
  1440.            struct rexp_node * a,
  1441.            struct rexp_node * b)
  1442. #else
  1443. RX_DECL struct rexp_node *
  1444. rx_mk_r_alternate (rx, a, b)
  1445.      struct rx * rx;
  1446.      struct rexp_node * a;
  1447.      struct rexp_node * b;
  1448. #endif
  1449. {
  1450.   struct rexp_node * n = rexp_node (rx, r_alternate);
  1451.   if (n)
  1452.     {
  1453.       n->params.pair.left = a;
  1454.       n->params.pair.right = b;
  1455.     }
  1456.   return n;
  1457. }
  1458.  
  1459.  
  1460. #ifdef __STDC__
  1461. RX_DECL struct rexp_node *
  1462. rx_mk_r_opt (struct rx * rx,
  1463.          struct rexp_node * a)
  1464. #else
  1465. RX_DECL struct rexp_node *
  1466. rx_mk_r_opt (rx, a)
  1467.      struct rx * rx;
  1468.      struct rexp_node * a;
  1469. #endif
  1470. {
  1471.   struct rexp_node * n = rexp_node (rx, r_opt);
  1472.   if (n)
  1473.     {
  1474.       n->params.pair.left = a;
  1475.       n->params.pair.right = 0;
  1476.     }
  1477.   return n;
  1478. }
  1479.  
  1480.  
  1481. #ifdef __STDC__
  1482. RX_DECL struct rexp_node *
  1483. rx_mk_r_star (struct rx * rx,
  1484.           struct rexp_node * a)
  1485. #else
  1486. RX_DECL struct rexp_node *
  1487. rx_mk_r_star (rx, a)
  1488.      struct rx * rx;
  1489.      struct rexp_node * a;
  1490. #endif
  1491. {
  1492.   struct rexp_node * n = rexp_node (rx, r_star);
  1493.   if (n)
  1494.     {
  1495.       n->params.pair.left = a;
  1496.       n->params.pair.right = 0;
  1497.     }
  1498.   return n;
  1499. }
  1500.  
  1501.  
  1502. #ifdef __STDC__
  1503. RX_DECL struct rexp_node *
  1504. rx_mk_r_2phase_star (struct rx * rx,
  1505.              struct rexp_node * a,
  1506.              struct rexp_node * b)
  1507. #else
  1508. RX_DECL struct rexp_node *
  1509. rx_mk_r_2phase_star (rx, a, b)
  1510.      struct rx * rx;
  1511.      struct rexp_node * a;
  1512.      struct rexp_node * b;
  1513. #endif
  1514. {
  1515.   struct rexp_node * n = rexp_node (rx, r_2phase_star);
  1516.   if (n)
  1517.     {
  1518.       n->params.pair.left = a;
  1519.       n->params.pair.right = b;
  1520.     }
  1521.   return n;
  1522. }
  1523.  
  1524.  
  1525.  
  1526. #ifdef __STDC__
  1527. RX_DECL struct rexp_node *
  1528. rx_mk_r_side_effect (struct rx * rx,
  1529.              rx_side_effect a)
  1530. #else
  1531. RX_DECL struct rexp_node *
  1532. rx_mk_r_side_effect (rx, a)
  1533.      struct rx * rx;
  1534.      rx_side_effect a;
  1535. #endif
  1536. {
  1537.   struct rexp_node * n = rexp_node (rx, r_side_effect);
  1538.   if (n)
  1539.     {
  1540.       n->params.side_effect = a;
  1541.       n->params.pair.right = 0;
  1542.     }
  1543.   return n;
  1544. }
  1545.  
  1546.  
  1547. #ifdef __STDC__
  1548. RX_DECL struct rexp_node *
  1549. rx_mk_r_data  (struct rx * rx,
  1550.            void * a)
  1551. #else
  1552. RX_DECL struct rexp_node *
  1553. rx_mk_r_data  (rx, a)
  1554.      struct rx * rx;
  1555.      void * a;
  1556. #endif
  1557. {
  1558.   struct rexp_node * n = rexp_node (rx, r_data);
  1559.   if (n)
  1560.     {
  1561.       n->params.pair.left = a;
  1562.       n->params.pair.right = 0;
  1563.     }
  1564.   return n;
  1565. }
  1566.  
  1567.  
  1568. #ifdef __STDC__
  1569. RX_DECL void
  1570. rx_free_rexp (struct rx * rx, struct rexp_node * node)
  1571. #else
  1572. RX_DECL void
  1573. rx_free_rexp (rx, node)
  1574.      struct rx * rx;
  1575.      struct rexp_node * node;
  1576. #endif
  1577. {
  1578.   if (node)
  1579.     {
  1580.       switch (node->type)
  1581.     {
  1582.     case r_cset:
  1583.       if (node->params.cset)
  1584.         rx_free_cset (rx, node->params.cset);
  1585.  
  1586.     case r_side_effect:
  1587.       break;
  1588.       
  1589.     case r_concat:
  1590.     case r_alternate:
  1591.     case r_2phase_star:
  1592.     case r_opt:
  1593.     case r_star:
  1594.       rx_free_rexp (rx, node->params.pair.left);
  1595.       rx_free_rexp (rx, node->params.pair.right);
  1596.       break;
  1597.  
  1598.     case r_data:
  1599.       /* This shouldn't occur. */
  1600.       break;
  1601.     }
  1602.       chunk_free (&rexp_pool, (char *)node);
  1603.     }
  1604. }
  1605.  
  1606.  
  1607. #ifdef __STDC__
  1608. RX_DECL struct rexp_node * 
  1609. rx_copy_rexp (struct rx *rx,
  1610.        struct rexp_node *node)
  1611. #else
  1612. RX_DECL struct rexp_node * 
  1613. rx_copy_rexp (rx, node)
  1614.      struct rx *rx;
  1615.      struct rexp_node *node;
  1616. #endif
  1617. {
  1618.   if (!node)
  1619.     return 0;
  1620.   else
  1621.     {
  1622.       struct rexp_node *n = rexp_node (rx, node->type);
  1623.       if (!n)
  1624.     return 0;
  1625.       switch (node->type)
  1626.     {
  1627.     case r_cset:
  1628.       n->params.cset = rx_copy_cset (rx, node->params.cset);
  1629.       if (!n->params.cset)
  1630.         {
  1631.           rx_free_rexp (rx, n);
  1632.           return 0;
  1633.         }
  1634.       break;
  1635.  
  1636.     case r_side_effect:
  1637.       n->params.side_effect = node->params.side_effect;
  1638.       break;
  1639.  
  1640.     case r_concat:
  1641.     case r_alternate:
  1642.     case r_opt:
  1643.     case r_2phase_star:
  1644.     case r_star:
  1645.       n->params.pair.left =
  1646.         rx_copy_rexp (rx, node->params.pair.left);
  1647.       n->params.pair.right =
  1648.         rx_copy_rexp (rx, node->params.pair.right);
  1649.       if (   (node->params.pair.left && !n->params.pair.left)
  1650.           || (node->params.pair.right && !n->params.pair.right))
  1651.         {
  1652.           rx_free_rexp  (rx, n);
  1653.           return 0;
  1654.         }
  1655.       break;
  1656.     case r_data:
  1657.       /* shouldn't happen */
  1658.       break;
  1659.     }
  1660.       return n;
  1661.     }
  1662. }
  1663.  
  1664.  
  1665.  
  1666. /* This page: functions to build and destroy graphs that describe nfa's */
  1667.  
  1668. static DEF_POOL(state_pool, struct rx_nfa_state);
  1669.  
  1670. /* Constructs a new nfa node. */
  1671. #ifdef __STDC__
  1672. RX_DECL struct rx_nfa_state *
  1673. rx_nfa_state (struct rx *rx)
  1674. #else
  1675. RX_DECL struct rx_nfa_state *
  1676. rx_nfa_state (rx)
  1677.      struct rx *rx;
  1678. #endif
  1679. {
  1680.   struct rx_nfa_state * n = (struct rx_nfa_state *)chunk_malloc (&state_pool);
  1681.   if (!n)
  1682.     return 0;
  1683.   bzero (n, sizeof (*n));
  1684.   n->next = rx->nfa_states;
  1685.   rx->nfa_states = n;
  1686.   return n;
  1687. }
  1688.  
  1689.  
  1690. #ifdef __STDC__
  1691. RX_DECL void
  1692. rx_free_nfa_state (struct rx_nfa_state * n)
  1693. #else
  1694. RX_DECL void
  1695. rx_free_nfa_state (n)
  1696.   struct rx_nfa_state * n;
  1697. #endif
  1698. {
  1699.   chunk_free (&state_pool, (char *)n);
  1700. }
  1701.  
  1702.  
  1703. /* This looks up an nfa node, given a numeric id.  Numeric id's are
  1704.  * assigned after the nfa has been built.
  1705.  */
  1706. #ifdef __STDC__
  1707. RX_DECL struct rx_nfa_state * 
  1708. rx_id_to_nfa_state (struct rx * rx,
  1709.             int id)
  1710. #else
  1711. RX_DECL struct rx_nfa_state * 
  1712. rx_id_to_nfa_state (rx, id)
  1713.      struct rx * rx;
  1714.      int id;
  1715. #endif
  1716. {
  1717.   struct rx_nfa_state * n;
  1718.   for (n = rx->nfa_states; n; n = n->next)
  1719.     if (n->id == id)
  1720.       return n;
  1721.   return 0;
  1722. }
  1723.  
  1724.  
  1725. /* This adds an edge between two nodes, but doesn't initialize the 
  1726.  * edge label.
  1727.  */
  1728.  
  1729. static DEF_POOL(edge_pool, struct rx_nfa_edge);
  1730.  
  1731. #ifdef __STDC__
  1732. RX_DECL struct rx_nfa_edge * 
  1733. rx_nfa_edge (struct rx *rx,
  1734.          enum rx_nfa_etype type,
  1735.          struct rx_nfa_state *start,
  1736.          struct rx_nfa_state *dest)
  1737. #else
  1738. RX_DECL struct rx_nfa_edge * 
  1739. rx_nfa_edge (rx, type, start, dest)
  1740.      struct rx *rx;
  1741.      enum rx_nfa_etype type;
  1742.      struct rx_nfa_state *start;
  1743.      struct rx_nfa_state *dest;
  1744. #endif
  1745. {
  1746.   struct rx_nfa_edge *e = (struct rx_nfa_edge *)chunk_malloc (&edge_pool);
  1747.   if (!e)
  1748.     return 0;
  1749.   e->next = start->edges;
  1750.   start->edges = e;
  1751.   e->type = type;
  1752.   e->dest = dest;
  1753.   return e;
  1754. }
  1755.  
  1756.  
  1757. #ifdef __STDC__
  1758. RX_DECL void
  1759. rx_free_nfa_edge (struct rx_nfa_edge * e)
  1760. #else
  1761. RX_DECL void
  1762. rx_free_nfa_edge (e)
  1763.      struct rx_nfa_edge * e;
  1764. #endif
  1765. {
  1766.   chunk_free (&edge_pool, (char *)e);
  1767. }
  1768.  
  1769.  
  1770. /* This constructs a POSSIBLE_FUTURE, which is a kind epsilon-closure
  1771.  * of an NFA.  These are added to an nfa automaticly by eclose_nfa.
  1772.  */  
  1773.  
  1774. static DEF_POOL(pf_pool, struct rx_possible_future);
  1775.  
  1776. #ifdef __STDC__
  1777. static struct rx_possible_future * 
  1778. rx_possible_future (struct rx * rx,
  1779.          struct rx_se_list * effects)
  1780. #else
  1781. static struct rx_possible_future * 
  1782. rx_possible_future (rx, effects)
  1783.      struct rx * rx;
  1784.      struct rx_se_list * effects;
  1785. #endif
  1786. {
  1787.   struct rx_possible_future *ec = ((struct rx_possible_future *)
  1788.                 chunk_malloc (&pf_pool));
  1789.   if (!ec)
  1790.     return 0;
  1791.   ec->destset = 0;
  1792.   ec->next = 0;
  1793.   ec->effects = effects;
  1794.   return ec;
  1795. }
  1796.  
  1797.  
  1798. #ifdef __STDC__
  1799. static void
  1800. rx_free_possible_future (struct rx_possible_future * pf)
  1801. #else
  1802. static void
  1803. rx_free_possible_future (pf)
  1804.      struct rx_possible_future * pf;
  1805. #endif
  1806. {
  1807.   chunk_free (&pf_pool, (char *)pf);
  1808. }
  1809.  
  1810.  
  1811. #ifdef __STDC__
  1812. RX_DECL void
  1813. rx_free_nfa (struct rx *rx)
  1814. #else
  1815. RX_DECL void
  1816. rx_free_nfa (rx)
  1817.      struct rx *rx;
  1818. #endif
  1819. {
  1820.   while (rx->nfa_states)
  1821.     {
  1822.       while (rx->nfa_states->edges)
  1823.     {
  1824.       switch (rx->nfa_states->edges->type)
  1825.         {
  1826.         case ne_cset:
  1827.           rx_free_cset (rx, rx->nfa_states->edges->params.cset);
  1828.           break;
  1829.         default:
  1830.           break;
  1831.         }
  1832.       {
  1833.         struct rx_nfa_edge * e;
  1834.         e = rx->nfa_states->edges;
  1835.         rx->nfa_states->edges = rx->nfa_states->edges->next;
  1836.         rx_free_nfa_edge (e);
  1837.       }
  1838.     } /* while (rx->nfa_states->edges) */
  1839.       {
  1840.     /* Iterate over the partial epsilon closures of rx->nfa_states */
  1841.     struct rx_possible_future * pf = rx->nfa_states->futures;
  1842.     while (pf)
  1843.       {
  1844.         struct rx_possible_future * pft = pf;
  1845.         pf = pf->next;
  1846.         rx_free_possible_future (pft);
  1847.       }
  1848.       }
  1849.       {
  1850.     struct rx_nfa_state *n;
  1851.     n = rx->nfa_states;
  1852.     rx->nfa_states = rx->nfa_states->next;
  1853.     rx_free_nfa_state (n);
  1854.       }
  1855.     }
  1856. }
  1857.  
  1858.  
  1859.  
  1860. /* This page: translating a pattern expression in to an nfa and doing the 
  1861.  * static part of the nfa->super-nfa translation.
  1862.  */
  1863.  
  1864. /* This is the thompson regexp->nfa algorithm. */
  1865. #ifdef __STDC__
  1866. RX_DECL int
  1867. rx_build_nfa (struct rx *rx,
  1868.           struct rexp_node *rexp,
  1869.           struct rx_nfa_state **start,
  1870.           struct rx_nfa_state **end)
  1871. #else
  1872. RX_DECL int
  1873. rx_build_nfa (rx, rexp, start, end)
  1874.      struct rx *rx;
  1875.      struct rexp_node *rexp;
  1876.      struct rx_nfa_state **start;
  1877.      struct rx_nfa_state **end;
  1878. #endif
  1879. {
  1880.   struct rx_nfa_edge *edge;
  1881.  
  1882.   /* Start & end nodes may have been allocated by the caller. */
  1883.   *start = *start ? *start : rx_nfa_state (rx);
  1884.  
  1885.   if (!*start)
  1886.     return 0;
  1887.  
  1888.   if (!rexp)
  1889.     {
  1890.       *end = *start;
  1891.       return 1;
  1892.     }
  1893.  
  1894.   *end = *end ? *end : rx_nfa_state (rx);
  1895.  
  1896.   if (!*end)
  1897.     {
  1898.       rx_free_nfa_state (*start);
  1899.       return 0;
  1900.     }
  1901.  
  1902.   switch (rexp->type)
  1903.     {
  1904.     case r_data:
  1905.       return 0;
  1906.       break;
  1907.     case r_cset:
  1908.       edge = rx_nfa_edge (rx, ne_cset, *start, *end);
  1909.       if (!edge)
  1910.     return 0;
  1911.       edge->params.cset = rx_copy_cset (rx, rexp->params.cset);
  1912.       if (!edge->params.cset)
  1913.     {
  1914.       rx_free_nfa_edge (edge);
  1915.       return 0;
  1916.     }
  1917.       return 1;
  1918.  
  1919.     case r_opt:
  1920.       return (rx_build_nfa (rx, rexp->params.pair.left, start, end)
  1921.           && rx_nfa_edge (rx, ne_epsilon, *start, *end));
  1922.  
  1923.     case r_star:
  1924.       {
  1925.     struct rx_nfa_state * star_start = 0;
  1926.     struct rx_nfa_state * star_end = 0;
  1927.     return (rx_build_nfa (rx, rexp->params.pair.left,
  1928.                   &star_start, &star_end)
  1929.         && star_start
  1930.         && star_end
  1931.         && rx_nfa_edge (rx, ne_epsilon, star_start, star_end)
  1932.         && rx_nfa_edge (rx, ne_epsilon, *start, star_start)
  1933.         && rx_nfa_edge (rx, ne_epsilon, star_end, *end)
  1934.  
  1935.         && rx_nfa_edge (rx, ne_epsilon, star_end, star_start));
  1936.       }
  1937.  
  1938.     case r_2phase_star:
  1939.       {
  1940.     struct rx_nfa_state * star_start = 0;
  1941.     struct rx_nfa_state * star_end = 0;
  1942.     struct rx_nfa_state * loop_exp_start = 0;
  1943.     struct rx_nfa_state * loop_exp_end = 0;
  1944.  
  1945.     return (rx_build_nfa (rx, rexp->params.pair.left,
  1946.                   &star_start, &star_end)
  1947.         && rx_build_nfa (rx, rexp->params.pair.right,
  1948.                  &loop_exp_start, &loop_exp_end)
  1949.         && star_start
  1950.         && star_end
  1951.         && loop_exp_end
  1952.         && loop_exp_start
  1953.         && rx_nfa_edge (rx, ne_epsilon, star_start, *end)
  1954.         && rx_nfa_edge (rx, ne_epsilon, *start, star_start)
  1955.         && rx_nfa_edge (rx, ne_epsilon, star_end, *end)
  1956.  
  1957.         && rx_nfa_edge (rx, ne_epsilon, star_end, loop_exp_start)
  1958.         && rx_nfa_edge (rx, ne_epsilon, loop_exp_end, star_start));
  1959.       }
  1960.  
  1961.  
  1962.     case r_concat:
  1963.       {
  1964.     struct rx_nfa_state *shared = 0;
  1965.     return
  1966.       (rx_build_nfa (rx, rexp->params.pair.left, start, &shared)
  1967.        && rx_build_nfa (rx, rexp->params.pair.right, &shared, end));
  1968.       }
  1969.  
  1970.     case r_alternate:
  1971.       {
  1972.     struct rx_nfa_state *ls = 0;
  1973.     struct rx_nfa_state *le = 0;
  1974.     struct rx_nfa_state *rs = 0;
  1975.     struct rx_nfa_state *re = 0;
  1976.     return (rx_build_nfa (rx, rexp->params.pair.left, &ls, &le)
  1977.         && rx_build_nfa (rx, rexp->params.pair.right, &rs, &re)
  1978.         && rx_nfa_edge (rx, ne_epsilon, *start, ls)
  1979.         && rx_nfa_edge (rx, ne_epsilon, *start, rs)
  1980.         && rx_nfa_edge (rx, ne_epsilon, le, *end)
  1981.         && rx_nfa_edge (rx, ne_epsilon, re, *end));
  1982.       }
  1983.  
  1984.     case r_side_effect:
  1985.       edge = rx_nfa_edge (rx, ne_side_effect, *start, *end);
  1986.       if (!edge)
  1987.     return 0;
  1988.       edge->params.side_effect = rexp->params.side_effect;
  1989.       return 1;
  1990.     };
  1991. }
  1992.  
  1993.  
  1994. /* NAME_RX->NFA_STATES identifies all nodes with non-epsilon transitions.
  1995.  * These nodes can occur in super-states.  All nodes are given an integer id.
  1996.  * The id is non-negative if the node has non-epsilon out-transitions, negative
  1997.  * otherwise (this is because we want the non-negative ids to be used as 
  1998.  * array indexes in a few places).
  1999.  */
  2000.  
  2001. #ifdef __STDC__
  2002. RX_DECL void
  2003. rx_name_nfa_states (struct rx *rx)
  2004. #else
  2005. RX_DECL void
  2006. rx_name_nfa_states (rx)
  2007.      struct rx *rx;
  2008. #endif
  2009. {
  2010.   struct rx_nfa_state *n = rx->nfa_states;
  2011.  
  2012.   rx->nodec = 0;
  2013.   rx->epsnodec = -1;
  2014.  
  2015.   while (n)
  2016.     {
  2017.       struct rx_nfa_edge *e = n->edges;
  2018.  
  2019.       if (n->is_start)
  2020.     n->eclosure_needed = 1;
  2021.  
  2022.       while (e)
  2023.     {
  2024.       switch (e->type)
  2025.         {
  2026.         case ne_epsilon:
  2027.         case ne_side_effect:
  2028.           break;
  2029.  
  2030.         case ne_cset:
  2031.           n->id = rx->nodec++;
  2032.           {
  2033.         struct rx_nfa_edge *from_n = n->edges;
  2034.         while (from_n)
  2035.           {
  2036.             from_n->dest->eclosure_needed = 1;
  2037.             from_n = from_n->next;
  2038.           }
  2039.           }
  2040.           goto cont;
  2041.         }
  2042.       e = e->next;
  2043.     }
  2044.       n->id = rx->epsnodec--;
  2045.     cont:
  2046.       n = n->next;
  2047.     }
  2048.   rx->epsnodec = -rx->epsnodec;
  2049. }
  2050.  
  2051.  
  2052. /* This page: data structures for the static part of the nfa->supernfa
  2053.  * translation.
  2054.  */
  2055.  
  2056. /* The next several functions compare, construct, etc. lists of side
  2057.  * effects.  See ECLOSE_NFA (below) for details.
  2058.  */
  2059.  
  2060. /* Ordering of rx_se_list
  2061.  * (-1, 0, 1 return value convention).
  2062.  */
  2063.  
  2064. #ifdef __STDC__
  2065. static int 
  2066. se_list_cmp (void * va, void * vb)
  2067. #else
  2068. static int 
  2069. se_list_cmp (va, vb)
  2070.      void * va;
  2071.      void * vb;
  2072. #endif
  2073. {
  2074.   struct rx_se_list * a = (struct rx_se_list *)va;
  2075.   struct rx_se_list * b = (struct rx_se_list *)vb;
  2076.  
  2077.   return ((va == vb)
  2078.       ? 0
  2079.       : (!va
  2080.          ? -1
  2081.          : (!vb
  2082.         ? 1
  2083.         : ((long)a->car < (long)b->car
  2084.            ? 1
  2085.            : ((long)a->car > (long)b->car
  2086.               ? -1
  2087.               : se_list_cmp ((void *)a->cdr, (void *)b->cdr))))));
  2088. }
  2089.  
  2090.  
  2091. #ifdef __STDC__
  2092. static int 
  2093. se_list_equal (void * va, void * vb)
  2094. #else
  2095. static int 
  2096. se_list_equal (va, vb)
  2097.      void * va;
  2098.      void * vb;
  2099. #endif
  2100. {
  2101.   return !(se_list_cmp (va, vb));
  2102. }
  2103.  
  2104. static struct rx_hash_rules se_list_hash_rules =
  2105. {
  2106.   se_list_equal,
  2107.   compiler_hash_alloc,
  2108.   compiler_free_hash,
  2109.   compiler_hash_item_alloc,
  2110.   compiler_free_hash_item
  2111. };
  2112.  
  2113. static DEF_POOL(sel_pool, struct rx_se_list);
  2114.  
  2115. #ifdef __STDC__
  2116. static struct rx_se_list * 
  2117. side_effect_cons (struct rx * rx,
  2118.           void * se, struct rx_se_list * list)
  2119. #else
  2120. static struct rx_se_list * 
  2121. side_effect_cons (rx, se, list)
  2122.      struct rx * rx;
  2123.      void * se;
  2124.      struct rx_se_list * list;
  2125. #endif
  2126. {
  2127.   struct rx_se_list * l = ((struct rx_se_list *)
  2128.                chunk_malloc (&sel_pool));
  2129.   if (!l)
  2130.     return 0;
  2131.   l->car = se;
  2132.   l->cdr = list;
  2133.   return l;
  2134. }
  2135.  
  2136.  
  2137. #ifdef __STDC__
  2138. static struct rx_se_list *
  2139. hash_cons_se_prog (struct rx * rx,
  2140.            struct rx_hash * memo,
  2141.            void * car, struct rx_se_list * cdr)
  2142. #else
  2143. static struct rx_se_list *
  2144. hash_cons_se_prog (rx, memo, car, cdr)
  2145.      struct rx * rx;
  2146.      struct rx_hash * memo;
  2147.      void * car;
  2148.      struct rx_se_list * cdr;
  2149. #endif
  2150. {
  2151.   long hash = (long)car ^ (long)cdr;
  2152.   struct rx_se_list template;
  2153.  
  2154.   template.car = car;
  2155.   template.cdr = cdr;
  2156.   {
  2157.     struct rx_hash_item * it = rx_hash_store (memo, hash,
  2158.                           (void *)&template,
  2159.                           &se_list_hash_rules);
  2160.     if (!it)
  2161.       return 0;
  2162.     if (it->data == (void *)&template)
  2163.       {
  2164.     struct rx_se_list * consed = ((struct rx_se_list *)
  2165.                       chunk_malloc (&sel_pool));
  2166.     *consed = template;
  2167.     it->data = (void *)consed;
  2168.       }
  2169.     return (struct rx_se_list *)it->data;
  2170.   }
  2171. }
  2172.      
  2173.  
  2174. #ifdef __STDC__
  2175. static struct rx_se_list *
  2176. hash_se_prog (struct rx * rx, struct rx_hash * memo, struct rx_se_list * prog)
  2177. #else
  2178. static struct rx_se_list *
  2179. hash_se_prog (rx, memo, prog)
  2180.      struct rx * rx;
  2181.      struct rx_hash * memo;
  2182.      struct rx_se_list * prog;
  2183. #endif
  2184. {
  2185.   struct rx_se_list * answer = 0;
  2186.   while (prog)
  2187.     {
  2188.       answer = hash_cons_se_prog (rx, memo, prog->car, answer);
  2189.       if (!answer)
  2190.     return 0;
  2191.       prog = prog->cdr;
  2192.     }
  2193.   return answer;
  2194. }
  2195.  
  2196.  
  2197. /* This page: more data structures for nfa->supernfa.  Specificly,
  2198.  * sets of nfa states.
  2199.  */
  2200.  
  2201. #ifdef __STDC__
  2202. static int 
  2203. nfa_set_cmp (void * va, void * vb)
  2204. #else
  2205. static int 
  2206. nfa_set_cmp (va, vb)
  2207.      void * va;
  2208.      void * vb;
  2209. #endif
  2210. {
  2211.   struct rx_nfa_state_set * a = (struct rx_nfa_state_set *)va;
  2212.   struct rx_nfa_state_set * b = (struct rx_nfa_state_set *)vb;
  2213.  
  2214.   return ((va == vb)
  2215.       ? 0
  2216.       : (!va
  2217.          ? -1
  2218.          : (!vb
  2219.         ? 1
  2220.         : (a->car->id < b->car->id
  2221.            ? 1
  2222.            : (a->car->id > b->car->id
  2223.               ? -1
  2224.               : nfa_set_cmp ((void *)a->cdr, (void *)b->cdr))))));
  2225. }
  2226.  
  2227. #ifdef __STDC__
  2228. static int 
  2229. nfa_set_equal (void * va, void * vb)
  2230. #else
  2231. static int 
  2232. nfa_set_equal (va, vb)
  2233.      void * va;
  2234.      void * vb;
  2235. #endif
  2236. {
  2237.   return !nfa_set_cmp (va, vb);
  2238. }
  2239.  
  2240. static struct rx_hash_rules nfa_set_hash_rules =
  2241. {
  2242.   nfa_set_equal,
  2243.   compiler_hash_alloc,
  2244.   compiler_free_hash,
  2245.   compiler_hash_item_alloc,
  2246.   compiler_free_hash_item
  2247. };
  2248.  
  2249.  
  2250. /* CONS -- again, sets with == elements are ==. */
  2251.  
  2252. static DEF_POOL(nfa_sets, struct rx_nfa_state_set);
  2253.  
  2254. #ifdef __STDC__
  2255. static struct rx_nfa_state_set * 
  2256. nfa_set_cons (struct rx * rx,
  2257.           struct rx_hash * memo, struct rx_nfa_state * state,
  2258.           struct rx_nfa_state_set * set)
  2259. #else
  2260. static struct rx_nfa_state_set * 
  2261. nfa_set_cons (rx, memo, state, set)
  2262.      struct rx * rx;
  2263.      struct rx_hash * memo;
  2264.      struct rx_nfa_state * state;
  2265.      struct rx_nfa_state_set * set;
  2266. #endif
  2267. {
  2268.   struct rx_nfa_state_set template;
  2269.   struct rx_hash_item * node;
  2270.   template.car = state;
  2271.   template.cdr = set;
  2272.   node = rx_hash_store (memo,
  2273.             (((long)state) >> 8) ^ (long)set,
  2274.             &template, &nfa_set_hash_rules);
  2275.   if (!node)
  2276.     return 0;
  2277.   if (node->data == &template)
  2278.     {
  2279.       struct rx_nfa_state_set * l = ((struct rx_nfa_state_set *)
  2280.                   chunk_malloc (&nfa_sets));
  2281.       node->data = (void *) l;
  2282.       if (!l)
  2283.     return 0;
  2284.       *l = template;
  2285.     }
  2286.   return (struct rx_nfa_state_set *)node->data;
  2287. }
  2288.  
  2289.  
  2290. #ifdef __STDC__
  2291. static struct rx_nfa_state_set * 
  2292. nfa_set_enjoin (struct rx * rx,
  2293.         struct rx_hash * memo, struct rx_nfa_state * state,
  2294.         struct rx_nfa_state_set * set)
  2295. #else
  2296. static struct rx_nfa_state_set * 
  2297. nfa_set_enjoin (rx, memo, state, set)
  2298.      struct rx * rx;
  2299.      struct rx_hash * memo;
  2300.      struct rx_nfa_state * state;
  2301.      struct rx_nfa_state_set * set;
  2302. #endif
  2303. {
  2304.   if (!set || state->id < set->car->id)
  2305.     return nfa_set_cons (rx, memo, state, set);
  2306.   if (state->id == set->car->id)
  2307.     return set;
  2308.   else
  2309.     {
  2310.       struct rx_nfa_state_set * newcdr
  2311.     = nfa_set_enjoin (rx, memo, state, set->cdr);
  2312.       if (newcdr != set->cdr)
  2313.     set = nfa_set_cons (rx, memo, set->car, newcdr);
  2314.       return set;
  2315.     }
  2316. }
  2317.  
  2318.  
  2319.  
  2320. /* This page: computing epsilon closures.  The closures aren't total.
  2321.  * Each node's closures are partitioned according to the side effects entailed
  2322.  * along the epsilon edges.  Return true on success.
  2323.  */ 
  2324.  
  2325. struct eclose_frame
  2326. {
  2327.   struct rx_se_list *prog_backwards;
  2328. };
  2329.  
  2330.  
  2331. #ifdef __STDC__
  2332. static int 
  2333. eclose_node (struct rx *rx, struct rx_nfa_state *outnode,
  2334.          struct rx_nfa_state *node, struct eclose_frame *frame)
  2335. #else
  2336. static int 
  2337. eclose_node (rx, outnode, node, frame)
  2338.      struct rx *rx;
  2339.      struct rx_nfa_state *outnode;
  2340.      struct rx_nfa_state *node;
  2341.      struct eclose_frame *frame;
  2342. #endif
  2343. {
  2344.   struct rx_nfa_edge *e = node->edges;
  2345.  
  2346.   /* For each node, we follow all epsilon paths to build the closure.
  2347.    * The closure omits nodes that have only epsilon edges.
  2348.    * The closure is split into partial closures -- all the states in
  2349.    * a partial closure are reached by crossing the same list of
  2350.    * of side effects (though not necessarily the same path).
  2351.    */
  2352.   if (node->mark)
  2353.     return 1;
  2354.   node->mark = 1;
  2355.  
  2356.   if (node->id >= 0 || node->is_final)
  2357.     {
  2358.       struct rx_possible_future **ec;
  2359.       struct rx_se_list * prog_in_order
  2360.     = ((struct rx_se_list *)hash_se_prog (rx,
  2361.                           &rx->se_list_memo,
  2362.                           frame->prog_backwards));
  2363.       int cmp;
  2364.  
  2365.       ec = &outnode->futures;
  2366.  
  2367.       while (*ec)
  2368.     {
  2369.       cmp = se_list_cmp ((void *)(*ec)->effects, (void *)prog_in_order);
  2370.       if (cmp <= 0)
  2371.         break;
  2372.       ec = &(*ec)->next;
  2373.     }
  2374.       if (!*ec || (cmp < 0))
  2375.     {
  2376.       struct rx_possible_future * saved = *ec;
  2377.       *ec = rx_possible_future (rx, prog_in_order);
  2378.       (*ec)->next = saved;
  2379.       if (!*ec)
  2380.         return 0;
  2381.     }
  2382.       if (node->id >= 0)
  2383.     {
  2384.       (*ec)->destset = nfa_set_enjoin (rx, &rx->set_list_memo,
  2385.                        node, (*ec)->destset);
  2386.       if (!(*ec)->destset)
  2387.         return 0;
  2388.     }
  2389.     }
  2390.  
  2391.   while (e)
  2392.     {
  2393.       switch (e->type)
  2394.     {
  2395.     case ne_epsilon:
  2396.       if (!eclose_node (rx, outnode, e->dest, frame))
  2397.         return 0;
  2398.       break;
  2399.     case ne_side_effect:
  2400.       {
  2401.         frame->prog_backwards = side_effect_cons (rx, 
  2402.                               e->params.side_effect,
  2403.                               frame->prog_backwards);
  2404.         if (!frame->prog_backwards)
  2405.           return 0;
  2406.         if (!eclose_node (rx, outnode, e->dest, frame))
  2407.           return 0;
  2408.         {
  2409.           struct rx_se_list * dying = frame->prog_backwards;
  2410.           frame->prog_backwards = frame->prog_backwards->cdr;
  2411.           chunk_free (&sel_pool, (char *)dying);
  2412.         }
  2413.         break;
  2414.       }
  2415.     default:
  2416.       break;
  2417.     }
  2418.       e = e->next;
  2419.     }
  2420.   node->mark = 0;
  2421.   return 1;
  2422. }
  2423.  
  2424.  
  2425. #ifdef __STDC__
  2426. RX_DECL int 
  2427. rx_eclose_nfa (struct rx *rx)
  2428. #else
  2429. RX_DECL int 
  2430. rx_eclose_nfa (rx)
  2431.      struct rx *rx;
  2432. #endif
  2433. {
  2434.   struct rx_nfa_state *n = rx->nfa_states;
  2435.   struct eclose_frame frame;
  2436.   static int rx_id = 0;
  2437.   
  2438.   frame.prog_backwards = 0;
  2439.   rx->rx_id = rx_id++;
  2440.   bzero (&rx->se_list_memo, sizeof (rx->se_list_memo));
  2441.   bzero (&rx->set_list_memo, sizeof (rx->set_list_memo));
  2442.   while (n)
  2443.     {
  2444.       n->futures = 0;
  2445.       if (n->eclosure_needed && !eclose_node (rx, n, n, &frame))
  2446.     return 0;
  2447.       /* clear_marks (rx); */
  2448.       n = n->next;
  2449.     }
  2450.   return 1;
  2451. }
  2452.  
  2453.  
  2454. /* This deletes epsilon edges from an NFA.  After running eclose_node,
  2455.  * we have no more need for these edges.  They are removed to simplify
  2456.  * further operations on the NFA.
  2457.  */
  2458.  
  2459. #ifdef __STDC__
  2460. RX_DECL void 
  2461. rx_delete_epsilon_transitions (struct rx *rx)
  2462. #else
  2463. RX_DECL void 
  2464. rx_delete_epsilon_transitions (rx)
  2465.      struct rx *rx;
  2466. #endif
  2467. {
  2468.   struct rx_nfa_state *n = rx->nfa_states;
  2469.   struct rx_nfa_edge **e;
  2470.  
  2471.   while (n)
  2472.     {
  2473.       e = &n->edges;
  2474.       while (*e)
  2475.     {
  2476.       struct rx_nfa_edge *t;
  2477.       switch ((*e)->type)
  2478.         {
  2479.         case ne_epsilon:
  2480.         case ne_side_effect:
  2481.           t = *e;
  2482.           *e = t->next;
  2483.           rx_free_nfa_edge (t);
  2484.           break;
  2485.  
  2486.         default:
  2487.           e = &(*e)->next;
  2488.           break;
  2489.         }
  2490.     }
  2491.       n = n->next;
  2492.     }
  2493. }
  2494.  
  2495.  
  2496. /* This page: storing the nfa in a contiguous region of memory for
  2497.  * subsequent conversion to a super-nfa.
  2498.  */
  2499.  
  2500.  
  2501. /* This is for qsort on an array of nfa_states. The order
  2502.  * is based on state ids and goes 
  2503.  *        [0...MAX][MIN..-1] where (MAX>=0) and (MIN<0)
  2504.  * This way, positive ids double as array indices.
  2505.  */
  2506.  
  2507. #ifdef __STDC__
  2508. static int 
  2509. nfacmp (void * va, void * vb)
  2510. #else
  2511. static int 
  2512. nfacmp (va, vb)
  2513.      void * va;
  2514.      void * vb;
  2515. #endif
  2516. {
  2517.   struct rx_nfa_state **a = (struct rx_nfa_state **)va;
  2518.   struct rx_nfa_state **b = (struct rx_nfa_state **)vb;
  2519.   return (*a == *b        /* &&&& 3.18 */
  2520.       ? 0
  2521.       : (((*a)->id < 0) == ((*b)->id < 0)
  2522.          ? (((*a)->id  < (*b)->id) ? -1 : 1)
  2523.          : (((*a)->id < 0)
  2524.         ? 1 : -1)));
  2525. }
  2526.  
  2527. #ifdef __STDC__
  2528. static int 
  2529. count_hash_nodes (struct rx_hash * st)
  2530. #else
  2531. static int 
  2532. count_hash_nodes (st)
  2533.      struct rx_hash * st;
  2534. #endif
  2535. {
  2536.   int x;
  2537.   int count = 0;
  2538.   for (x = 0; x < 13; ++x)
  2539.     count += ((st->children[x])
  2540.           ? count_hash_nodes (st->children[x])
  2541.           : st->bucket_size[x]);
  2542.   
  2543.   return count;
  2544. }
  2545.  
  2546.  
  2547. #ifdef __STDC__
  2548. static void 
  2549. se_memo_freer (struct rx_hash_item * node)
  2550. #else
  2551. static void 
  2552. se_memo_freer (node)
  2553.      struct rx_hash_item * node;
  2554. #endif
  2555. {
  2556.   chunk_free (&sel_pool, (char *)node->data);
  2557. }
  2558.  
  2559.  
  2560. #ifdef __STDC__
  2561. static void 
  2562. nfa_set_freer (struct rx_hash_item * node)
  2563. #else
  2564. static void 
  2565. nfa_set_freer (node)
  2566.      struct rx_hash_item * node;
  2567. #endif
  2568. {
  2569.   chunk_free (&nfa_sets, (char *)node->data);
  2570. }
  2571.  
  2572.  
  2573. /* This copies an entire NFA into a single malloced block of memory.
  2574.  * Mostly this is for compatability with regex.c, though it is convenient
  2575.  * to have the nfa nodes in an array.
  2576.  */
  2577.  
  2578. #ifdef __STDC__
  2579. RX_DECL int 
  2580. rx_compactify_nfa (struct rx *rx,
  2581.            void **mem, unsigned long *size)
  2582. #else
  2583. RX_DECL int 
  2584. rx_compactify_nfa (rx, mem, size)
  2585.      struct rx *rx;
  2586.      void **mem;
  2587.      unsigned long *size;
  2588. #endif
  2589. {
  2590.   int total_nodec;
  2591.   struct rx_nfa_state *n;
  2592.   int edgec = 0;
  2593.   int eclosec = 0;
  2594.   int se_list_consc = count_hash_nodes (&rx->se_list_memo);
  2595.   int nfa_setc = count_hash_nodes (&rx->set_list_memo);
  2596.   unsigned long total_size;
  2597.  
  2598.   /* This takes place in two stages.   First, the total size of the
  2599.    * nfa is computed, then structures are copied.  
  2600.    */   
  2601.   n = rx->nfa_states;
  2602.   total_nodec = 0;
  2603.   while (n)
  2604.     {
  2605.       struct rx_nfa_edge *e = n->edges;
  2606.       struct rx_possible_future *ec = n->futures;
  2607.       ++total_nodec;
  2608.       while (e)
  2609.     {
  2610.       ++edgec;
  2611.       e = e->next;
  2612.     }
  2613.       while (ec)
  2614.     {
  2615.       ++eclosec;
  2616.       ec = ec->next;
  2617.     }
  2618.       n = n->next;
  2619.     }
  2620.  
  2621.   total_size = (total_nodec * sizeof (struct rx_nfa_state)
  2622.         + edgec * rx_sizeof_bitset (rx->local_cset_size)
  2623.         + edgec * sizeof (struct rx_nfa_edge)
  2624.         + nfa_setc * sizeof (struct rx_nfa_state_set)
  2625.         + eclosec * sizeof (struct rx_possible_future)
  2626.         + se_list_consc * sizeof (struct rx_se_list)
  2627.         + rx->reserved);
  2628.  
  2629.   if (total_size > *size)
  2630.     {
  2631.       *mem = remalloc (*mem, total_size);
  2632.       if (*mem)
  2633.     *size = total_size;
  2634.       else
  2635.     return 0;
  2636.     }
  2637.   /* Now we've allocated the memory; this copies the NFA. */
  2638.   {
  2639.     static struct rx_nfa_state **scratch = 0;
  2640.     static int scratch_alloc = 0;
  2641.     struct rx_nfa_state *state_base = (struct rx_nfa_state *) * mem;
  2642.     struct rx_nfa_state *new_state = state_base;
  2643.     struct rx_nfa_edge *new_edge =
  2644.       (struct rx_nfa_edge *)
  2645.     ((char *) state_base + total_nodec * sizeof (struct rx_nfa_state));
  2646.     struct rx_se_list * new_se_list =
  2647.       (struct rx_se_list *)
  2648.     ((char *)new_edge + edgec * sizeof (struct rx_nfa_edge));
  2649.     struct rx_possible_future *new_close =
  2650.       ((struct rx_possible_future *)
  2651.        ((char *) new_se_list
  2652.     + se_list_consc * sizeof (struct rx_se_list)));
  2653.     struct rx_nfa_state_set * new_nfa_set =
  2654.       ((struct rx_nfa_state_set *)
  2655.        ((char *)new_close + eclosec * sizeof (struct rx_possible_future)));
  2656.     char *new_bitset =
  2657.       ((char *) new_nfa_set + nfa_setc * sizeof (struct rx_nfa_state_set));
  2658.     int x;
  2659.     struct rx_nfa_state *n;
  2660.  
  2661.     if (scratch_alloc < total_nodec)
  2662.       {
  2663.     scratch = ((struct rx_nfa_state **)
  2664.            remalloc (scratch, total_nodec * sizeof (*scratch)));
  2665.     if (scratch)
  2666.       scratch_alloc = total_nodec;
  2667.     else
  2668.       {
  2669.         scratch_alloc = 0;
  2670.         return 0;
  2671.       }
  2672.       }
  2673.  
  2674.     for (x = 0, n = rx->nfa_states; n; n = n->next)
  2675.       scratch[x++] = n;
  2676.  
  2677.     qsort (scratch, total_nodec,
  2678.        sizeof (struct rx_nfa_state *), (int (*)())nfacmp);
  2679.  
  2680.     for (x = 0; x < total_nodec; ++x)
  2681.       {
  2682.     struct rx_possible_future *eclose = scratch[x]->futures;
  2683.     struct rx_nfa_edge *edge = scratch[x]->edges;
  2684.     struct rx_nfa_state *cn = new_state++;
  2685.     cn->futures = 0;
  2686.     cn->edges = 0;
  2687.     cn->next = (x == total_nodec - 1) ? 0 : (cn + 1);
  2688.     cn->id = scratch[x]->id;
  2689.     cn->is_final = scratch[x]->is_final;
  2690.     cn->is_start = scratch[x]->is_start;
  2691.     cn->mark = 0;
  2692.     while (edge)
  2693.       {
  2694.         int indx = (edge->dest->id < 0
  2695.              ? (total_nodec + edge->dest->id)
  2696.              : edge->dest->id);
  2697.         struct rx_nfa_edge *e = new_edge++;
  2698.         rx_Bitset cset = (rx_Bitset) new_bitset;
  2699.         new_bitset += rx_sizeof_bitset (rx->local_cset_size);
  2700.         rx_bitset_null (rx->local_cset_size, cset);
  2701.         rx_bitset_union (rx->local_cset_size, cset, edge->params.cset);
  2702.         e->next = cn->edges;
  2703.         cn->edges = e;
  2704.         e->type = edge->type;
  2705.         e->dest = state_base + indx;
  2706.         e->params.cset = cset;
  2707.         edge = edge->next;
  2708.       }
  2709.     while (eclose)
  2710.       {
  2711.         struct rx_possible_future *ec = new_close++;
  2712.         struct rx_hash_item * sp;
  2713.         struct rx_se_list ** sepos;
  2714.         struct rx_se_list * sesrc;
  2715.         struct rx_nfa_state_set * destlst;
  2716.         struct rx_nfa_state_set ** destpos;
  2717.         ec->next = cn->futures;
  2718.         cn->futures = ec;
  2719.         for (sepos = &ec->effects, sesrc = eclose->effects;
  2720.          sesrc;
  2721.          sesrc = sesrc->cdr, sepos = &(*sepos)->cdr)
  2722.           {
  2723.         sp = rx_hash_find (&rx->se_list_memo,
  2724.                    (long)sesrc->car ^ (long)sesrc->cdr,
  2725.                    sesrc, &se_list_hash_rules);
  2726.         if (sp->binding)
  2727.           {
  2728.             sesrc = (struct rx_se_list *)sp->binding;
  2729.             break;
  2730.           }
  2731.         *new_se_list = *sesrc;
  2732.         sp->binding = (void *)new_se_list;
  2733.         *sepos = new_se_list;
  2734.         ++new_se_list;
  2735.           }
  2736.         *sepos = sesrc;
  2737.         for (destpos = &ec->destset, destlst = eclose->destset;
  2738.          destlst;
  2739.          destpos = &(*destpos)->cdr, destlst = destlst->cdr)
  2740.           {
  2741.         sp = rx_hash_find (&rx->set_list_memo,
  2742.                    ((((long)destlst->car) >> 8)
  2743.                     ^ (long)destlst->cdr),
  2744.                    destlst, &nfa_set_hash_rules);
  2745.         if (sp->binding)
  2746.           {
  2747.             destlst = (struct rx_nfa_state_set *)sp->binding;
  2748.             break;
  2749.           }
  2750.         *new_nfa_set = *destlst;
  2751.         new_nfa_set->car = state_base + destlst->car->id;
  2752.         sp->binding = (void *)new_nfa_set;
  2753.         *destpos = new_nfa_set;
  2754.         ++new_nfa_set;
  2755.           }
  2756.         *destpos = destlst;
  2757.         eclose = eclose->next;
  2758.       }
  2759.       }
  2760.   }
  2761.   rx_free_hash_table (&rx->se_list_memo, se_memo_freer, &se_list_hash_rules);
  2762.   bzero (&rx->se_list_memo, sizeof (rx->se_list_memo));
  2763.   rx_free_hash_table (&rx->set_list_memo, nfa_set_freer, &nfa_set_hash_rules);
  2764.   bzero (&rx->set_list_memo, sizeof (rx->set_list_memo));
  2765.  
  2766.   rx_free_nfa (rx);
  2767.   rx->nfa_states = (struct rx_nfa_state *)*mem;
  2768.   return 1;
  2769. }
  2770.  
  2771.  
  2772. /* The functions in the next several pages define the lazy-NFA-conversion used
  2773.  * by matchers.  The input to this construction is an NFA such as 
  2774.  * is built by compactify_nfa (rx.c).  The output is the superNFA.
  2775.  */
  2776.  
  2777.  
  2778. /* Match engines can use arbitrary values for opcodes.  So, the parse tree 
  2779.  * is built using instructions names (enum rx_opcode), but the superstate
  2780.  * nfa is populated with mystery opcodes (void *).
  2781.  *
  2782.  * For convenience, here is an id table.  The opcodes are == to their inxs
  2783.  *
  2784.  * The lables in re_search_2 would make good values for instructions.
  2785.  */
  2786.  
  2787. void * rx_id_instruction_table[rx_num_instructions] =
  2788. {
  2789.   (void *) rx_backtrack_point,
  2790.   (void *) rx_do_side_effects,
  2791.   (void *) rx_cache_miss,
  2792.   (void *) rx_next_char,
  2793.   (void *) rx_backtrack,
  2794.   (void *) rx_error_inx
  2795. };
  2796.  
  2797.  
  2798.  
  2799. /* Memory mgt. for superstate graphs. */
  2800.  
  2801. #ifdef __STDC__
  2802. static char *
  2803. rx_cache_malloc (struct rx_cache * cache, int bytes)
  2804. #else
  2805. static char *
  2806. rx_cache_malloc (cache, bytes)
  2807.      struct rx_cache * cache;
  2808.      int bytes;
  2809. #endif
  2810. {
  2811.   while (cache->bytes_left < bytes)
  2812.     {
  2813.       if (cache->memory_pos)
  2814.     cache->memory_pos = cache->memory_pos->next;
  2815.       if (!cache->memory_pos)
  2816.     {
  2817.       cache->morecore (cache);
  2818.       if (!cache->memory_pos)
  2819.         return 0;
  2820.     }
  2821.       cache->bytes_left = cache->memory_pos->bytes;
  2822.       cache->memory_addr = ((char *)cache->memory_pos
  2823.                 + sizeof (struct rx_blocklist));
  2824.     }
  2825.   cache->bytes_left -= bytes;
  2826.   {
  2827.     char * addr = cache->memory_addr;
  2828.     cache->memory_addr += bytes;
  2829.     return addr;
  2830.   }
  2831. }
  2832.  
  2833. #ifdef __STDC__
  2834. static void
  2835. rx_cache_free (struct rx_cache * cache,
  2836.            struct rx_freelist ** freelist, char * mem)
  2837. #else
  2838. static void
  2839. rx_cache_free (cache, freelist, mem)
  2840.      struct rx_cache * cache;
  2841.      struct rx_freelist ** freelist;
  2842.      char * mem;
  2843. #endif
  2844. {
  2845.   struct rx_freelist * it = (struct rx_freelist *)mem;
  2846.   it->next = *freelist;
  2847.   *freelist = it;
  2848. }
  2849.  
  2850.  
  2851. /* The partially instantiated superstate graph has a transition 
  2852.  * table at every node.  There is one entry for every character.
  2853.  * This fills in the transition for a set.
  2854.  */
  2855. #ifdef __STDC__
  2856. static void 
  2857. install_transition (struct rx_superstate *super,
  2858.             struct rx_inx *answer, rx_Bitset trcset) 
  2859. #else
  2860. static void 
  2861. install_transition (super, answer, trcset)
  2862.      struct rx_superstate *super;
  2863.      struct rx_inx *answer;
  2864.      rx_Bitset trcset;
  2865. #endif
  2866. {
  2867.   struct rx_inx * transitions = super->transitions;
  2868.   int chr;
  2869.   for (chr = 0; chr < 256; )
  2870.     if (!*trcset)
  2871.       {
  2872.     ++trcset;
  2873.     chr += 32;
  2874.       }
  2875.     else
  2876.       {
  2877.     RX_subset sub = *trcset;
  2878.     RX_subset mask = 1;
  2879.     int bound = chr + 32;
  2880.     while (chr < bound)
  2881.       {
  2882.         if (sub & mask)
  2883.           transitions [chr] = *answer;
  2884.         ++chr;
  2885.         mask <<= 1;
  2886.       }
  2887.     ++trcset;
  2888.       }
  2889. }
  2890.  
  2891.  
  2892. #if 1
  2893. static int
  2894. qlen (q)
  2895.      struct rx_superstate * q;
  2896. {
  2897.   int count = 1;
  2898.   struct rx_superstate * it;
  2899.   if (!q)
  2900.     return 0;
  2901.   for (it = q->next_recyclable; it != q; it = it->next_recyclable)
  2902.     ++count;
  2903.   return count;
  2904. }
  2905.  
  2906. static void
  2907. check_cache (cache)
  2908.      struct rx_cache * cache;
  2909. {
  2910.   struct rx_cache * you_fucked_up = 0;
  2911.   int total = cache->superstates;
  2912.   int semi = cache->semifree_superstates;
  2913.   if (semi != qlen (cache->semifree_superstate))
  2914.     check_cache (you_fucked_up);
  2915.   if ((total - semi) != qlen (cache->lru_superstate))
  2916.     check_cache (you_fucked_up);
  2917. }
  2918. #endif
  2919.  
  2920. #ifdef __STDC__
  2921. static void
  2922. semifree_superstate (struct rx_cache * cache)
  2923. #else
  2924. static void
  2925. semifree_superstate (cache)
  2926.      struct rx_cache * cache;
  2927. #endif
  2928. {
  2929.   int disqualified = cache->semifree_superstates;
  2930.   if (disqualified == cache->superstates)
  2931.     return;
  2932.   while (cache->lru_superstate->locks)
  2933.     {
  2934.       cache->lru_superstate = cache->lru_superstate->next_recyclable;
  2935.       ++disqualified;
  2936.       if (disqualified == cache->superstates)
  2937.     return;
  2938.     }
  2939.   {
  2940.     struct rx_superstate * it = cache->lru_superstate;
  2941.     it->next_recyclable->prev_recyclable = it->prev_recyclable;
  2942.     it->prev_recyclable->next_recyclable = it->next_recyclable;
  2943.     cache->lru_superstate = (it == it->next_recyclable
  2944.                  ? 0
  2945.                  : it->next_recyclable);
  2946.     if (!cache->semifree_superstate)
  2947.       {
  2948.     cache->semifree_superstate = it;
  2949.     it->next_recyclable = it;
  2950.     it->prev_recyclable = it;
  2951.       }
  2952.     else
  2953.       {
  2954.     it->prev_recyclable = cache->semifree_superstate->prev_recyclable;
  2955.     it->next_recyclable = cache->semifree_superstate;
  2956.     it->prev_recyclable->next_recyclable = it;
  2957.     it->next_recyclable->prev_recyclable = it;
  2958.       }
  2959.     {
  2960.       struct rx_distinct_future *df;
  2961.       it->is_semifree = 1;
  2962.       ++cache->semifree_superstates;
  2963.       df = it->transition_refs;
  2964.       if (df)
  2965.     {
  2966.       df->prev_same_dest->next_same_dest = 0;
  2967.       for (df = it->transition_refs; df; df = df->next_same_dest)
  2968.         {
  2969.           df->future_frame.inx = cache->instruction_table[rx_cache_miss];
  2970.           df->future_frame.data = 0;
  2971.           df->future_frame.data_2 = (void *) df;
  2972.           /* If there are any NEXT-CHAR instruction frames that
  2973.            * refer to this state, we convert them to CACHE-MISS frames.
  2974.            */
  2975.           if (!(df->effects || df->edge->options->next_same_super_edge))
  2976.         install_transition (df->present, &df->future_frame,
  2977.                     df->edge->cset);
  2978.         }
  2979.       df = it->transition_refs;
  2980.       df->prev_same_dest->next_same_dest = df;
  2981.     }
  2982.     }
  2983.   }
  2984. }
  2985.  
  2986.  
  2987. #ifdef __STDC__
  2988. static void 
  2989. refresh_semifree_superstate (struct rx_cache * cache,
  2990.                  struct rx_superstate * super)
  2991. #else
  2992. static void 
  2993. refresh_semifree_superstate (cache, super)
  2994.      struct rx_cache * cache;
  2995.      struct rx_superstate * super;
  2996. #endif
  2997. {
  2998.   struct rx_distinct_future *df;
  2999.  
  3000.   if (super->transition_refs)
  3001.     {
  3002.       super->transition_refs->prev_same_dest->next_same_dest = 0; 
  3003.       for (df = super->transition_refs; df; df = df->next_same_dest)
  3004.     {
  3005.       df->future_frame.inx = cache->instruction_table[rx_next_char];
  3006.       df->future_frame.data = (void *) super->transitions;
  3007.       /* CACHE-MISS instruction frames that refer to this state,
  3008.        * must be converted to NEXT-CHAR frames.
  3009.        */
  3010.       if (!(df->effects || df->edge->options->next_same_super_edge))
  3011.         install_transition (df->present, &df->future_frame,
  3012.                 df->edge->cset);
  3013.     }
  3014.       super->transition_refs->prev_same_dest->next_same_dest
  3015.     = super->transition_refs;
  3016.     }
  3017.   if (cache->semifree_superstate == super)
  3018.     cache->semifree_superstate = (super->prev_recyclable == super
  3019.                   ? 0
  3020.                   : super->prev_recyclable);
  3021.   super->next_recyclable->prev_recyclable = super->prev_recyclable;
  3022.   super->prev_recyclable->next_recyclable = super->next_recyclable;
  3023.  
  3024.   if (!cache->lru_superstate)
  3025.     (cache->lru_superstate
  3026.      = super->next_recyclable
  3027.      = super->prev_recyclable
  3028.      = super);
  3029.   else
  3030.     {
  3031.       super->next_recyclable = cache->lru_superstate;
  3032.       super->prev_recyclable = cache->lru_superstate->prev_recyclable;
  3033.       super->next_recyclable->prev_recyclable = super;
  3034.       super->prev_recyclable->next_recyclable = super;
  3035.     }
  3036.   super->is_semifree = 0;
  3037.   --cache->semifree_superstates;
  3038. }
  3039.  
  3040. #ifdef __STDC__
  3041. static void
  3042. rx_refresh_this_superstate (struct rx_cache * cache, struct rx_superstate * superstate)
  3043. #else
  3044. static void
  3045. rx_refresh_this_superstate (cache, superstate)
  3046.      struct rx_cache * cache;
  3047.      struct rx_superstate * superstate;
  3048. #endif
  3049. {
  3050.   if (superstate->is_semifree)
  3051.     refresh_semifree_superstate (cache, superstate);
  3052.   else if (cache->lru_superstate == superstate)
  3053.     cache->lru_superstate = superstate->next_recyclable;
  3054.   else if (superstate != cache->lru_superstate->prev_recyclable)
  3055.     {
  3056.       superstate->next_recyclable->prev_recyclable
  3057.     = superstate->prev_recyclable;
  3058.       superstate->prev_recyclable->next_recyclable
  3059.     = superstate->next_recyclable;
  3060.       superstate->next_recyclable = cache->lru_superstate;
  3061.       superstate->prev_recyclable = cache->lru_superstate->prev_recyclable;
  3062.       superstate->next_recyclable->prev_recyclable = superstate;
  3063.       superstate->prev_recyclable->next_recyclable = superstate;
  3064.     }
  3065. }
  3066.  
  3067. #ifdef __STDC__
  3068. static void 
  3069. release_superset_low (struct rx_cache * cache,
  3070.              struct rx_superset *set)
  3071. #else
  3072. static void 
  3073. release_superset_low (cache, set)
  3074.      struct rx_cache * cache;
  3075.      struct rx_superset *set;
  3076. #endif
  3077. {
  3078.   if (!--set->refs)
  3079.     {
  3080.       if (set->cdr)
  3081.     release_superset_low (cache, set->cdr);
  3082.  
  3083.       rx_hash_free
  3084.     (rx_hash_find
  3085.      (&cache->superset_table,
  3086.       (unsigned long)set->car ^ set->id ^ (unsigned long)set->cdr,
  3087.       (void *)set,
  3088.       &cache->superset_hash_rules),
  3089.      &cache->superset_hash_rules);
  3090.       rx_cache_free (cache, &cache->free_supersets, (char *)set);
  3091.     }
  3092. }
  3093.  
  3094. /* This tries to add a new superstate to the superstate freelist.
  3095.  * It might, as a result, free some edge pieces or hash tables.
  3096.  * If nothing can be freed because too many locks are being held, fail.
  3097.  */
  3098.  
  3099. #ifdef __STDC__
  3100. static int
  3101. rx_really_free_superstate (struct rx_cache * cache)
  3102. #else
  3103. static int
  3104. rx_really_free_superstate (cache)
  3105.      struct rx_cache * cache;
  3106. #endif
  3107. {
  3108.   int locked_superstates = 0;
  3109.   struct rx_superstate * it;
  3110.  
  3111.   if (!cache->superstates)
  3112.     return 0;
  3113.  
  3114.   {
  3115.     /* This is a total guess.  The idea is that we should expect as
  3116.      * many misses as we've recently experienced.  I.e., cache->misses
  3117.      * should be the same as cache->semifree_superstates.
  3118.      */
  3119.     while ((cache->hits + cache->misses) > cache->superstates_allowed)
  3120.       {
  3121.     cache->hits >>= 1;
  3122.     cache->misses >>= 1;
  3123.       }
  3124.     if (  ((cache->hits + cache->misses) * cache->semifree_superstates)
  3125.     < (cache->superstates         * cache->misses))
  3126.       {
  3127.     semifree_superstate (cache);
  3128.     semifree_superstate (cache);
  3129.       }
  3130.   }
  3131.  
  3132.   while (cache->semifree_superstate && cache->semifree_superstate->locks)
  3133.     {
  3134.       refresh_semifree_superstate (cache, cache->semifree_superstate);
  3135.       ++locked_superstates;
  3136.       if (locked_superstates == cache->superstates)
  3137.     return 0;
  3138.     }
  3139.  
  3140.   if (cache->semifree_superstate)
  3141.     {
  3142.       it = cache->semifree_superstate;
  3143.       it->next_recyclable->prev_recyclable = it->prev_recyclable;
  3144.       it->prev_recyclable->next_recyclable = it->next_recyclable;
  3145.       cache->semifree_superstate = ((it == it->next_recyclable)
  3146.                     ? 0
  3147.                     : it->next_recyclable);
  3148.       --cache->semifree_superstates;
  3149.     }
  3150.   else
  3151.     {
  3152.       while (cache->lru_superstate->locks)
  3153.     {
  3154.       cache->lru_superstate = cache->lru_superstate->next_recyclable;
  3155.       ++locked_superstates;
  3156.       if (locked_superstates == cache->superstates)
  3157.         return 0;
  3158.     }
  3159.       it = cache->lru_superstate;
  3160.       it->next_recyclable->prev_recyclable = it->prev_recyclable;
  3161.       it->prev_recyclable->next_recyclable = it->next_recyclable;
  3162.       cache->lru_superstate = ((it == it->next_recyclable)
  3163.                     ? 0
  3164.                     : it->next_recyclable);
  3165.     }
  3166.  
  3167.   if (it->transition_refs)
  3168.     {
  3169.       struct rx_distinct_future *df;
  3170.       for (df = it->transition_refs,
  3171.        df->prev_same_dest->next_same_dest = 0;
  3172.        df;
  3173.        df = df->next_same_dest)
  3174.     {
  3175.       df->future_frame.inx = cache->instruction_table[rx_cache_miss];
  3176.       df->future_frame.data = 0;
  3177.       df->future_frame.data_2 = (void *) df;
  3178.       df->future = 0;
  3179.     }
  3180.       it->transition_refs->prev_same_dest->next_same_dest =
  3181.     it->transition_refs;
  3182.     }
  3183.   {
  3184.     struct rx_super_edge *tc = it->edges;
  3185.     while (tc)
  3186.       {
  3187.     struct rx_distinct_future * df;
  3188.     struct rx_super_edge *tct = tc->next;
  3189.     df = tc->options;
  3190.     while (df)
  3191.       {
  3192.         struct rx_distinct_future *dft = df;
  3193.         df = df->next_same_super_edge;
  3194.         
  3195.         
  3196.         if (dft->future && dft->future->transition_refs == dft)
  3197.           {
  3198.         dft->future->transition_refs = dft->next_same_dest;
  3199.         if (dft->future->transition_refs == dft)
  3200.           dft->future->transition_refs = 0;
  3201.           }
  3202.         dft->next_same_dest->prev_same_dest = dft->prev_same_dest;
  3203.         dft->prev_same_dest->next_same_dest = dft->next_same_dest;
  3204.         rx_cache_free (cache, &cache->free_discernable_futures,
  3205.                (char *)dft);
  3206.       }
  3207.     rx_cache_free (cache, &cache->free_transition_classes, (char *)tc);
  3208.     tc = tct;
  3209.       }
  3210.   }
  3211.   
  3212.   if (it->contents->superstate == it)
  3213.     it->contents->superstate = 0;
  3214.   release_superset_low (cache, it->contents);
  3215.   rx_cache_free (cache, &cache->free_superstates, (char *)it);
  3216.   --cache->superstates;
  3217.   return 1;
  3218. }
  3219.  
  3220. #ifdef __STDC__
  3221. static char *
  3222. rx_cache_get (struct rx_cache * cache,
  3223.           struct rx_freelist ** freelist)
  3224. #else
  3225. static char *
  3226. rx_cache_get (cache, freelist)
  3227.      struct rx_cache * cache;
  3228.      struct rx_freelist ** freelist;
  3229. #endif
  3230. {
  3231.   while (!*freelist && rx_really_free_superstate (cache))
  3232.     ;
  3233.   if (!*freelist)
  3234.     return 0;
  3235.   {
  3236.     struct rx_freelist * it = *freelist;
  3237.     *freelist = it->next;
  3238.     return (char *)it;
  3239.   }
  3240. }
  3241.  
  3242. #ifdef __STDC__
  3243. static char *
  3244. rx_cache_malloc_or_get (struct rx_cache * cache,
  3245.             struct rx_freelist ** freelist, int bytes)
  3246. #else
  3247. static char *
  3248. rx_cache_malloc_or_get (cache, freelist, bytes)
  3249.      struct rx_cache * cache;
  3250.      struct rx_freelist ** freelist;
  3251.      int bytes;
  3252. #endif
  3253. {
  3254.   if (!*freelist)
  3255.     {
  3256.       char * answer = rx_cache_malloc (cache, bytes);
  3257.       if (answer)
  3258.     return answer;
  3259.     }
  3260.  
  3261.   return rx_cache_get (cache, freelist);
  3262. }
  3263.  
  3264. #ifdef __STDC__
  3265. static char *
  3266. rx_cache_get_superstate (struct rx_cache * cache)
  3267. #else
  3268. static char *
  3269. rx_cache_get_superstate (cache)
  3270.       struct rx_cache * cache;
  3271. #endif
  3272. {
  3273.   char * answer;
  3274.   int bytes = (   sizeof (struct rx_superstate)
  3275.            +  cache->local_cset_size * sizeof (struct rx_inx));
  3276.   if (!cache->free_superstates
  3277.       && (cache->superstates < cache->superstates_allowed))
  3278.     {
  3279.       answer = rx_cache_malloc (cache, bytes);
  3280.       if (answer)
  3281.     {
  3282.       ++cache->superstates;
  3283.       return answer;
  3284.     }
  3285.     }
  3286.   answer = rx_cache_get (cache, &cache->free_superstates);
  3287.   if (!answer)
  3288.     {
  3289.       answer = rx_cache_malloc (cache, bytes);
  3290.       if (answer)
  3291.     ++cache->superstates_allowed;
  3292.     }
  3293.   ++cache->superstates;
  3294.   return answer;
  3295. }
  3296.  
  3297.  
  3298.  
  3299. static int
  3300. supersetcmp (va, vb)
  3301.      void * va;
  3302.      void * vb;
  3303. {
  3304.   struct rx_superset * a = (struct rx_superset *)va;
  3305.   struct rx_superset * b = (struct rx_superset *)vb;
  3306.   return (   (a == b)
  3307.       || (a && b && (a->car == b->car) && (a->cdr == b->cdr)));
  3308. }
  3309.  
  3310.  
  3311. #ifdef __STDC__
  3312. static struct rx_hash_item *
  3313. superset_allocator (struct rx_hash_rules * rules, void * val)
  3314. #else
  3315. static struct rx_hash_item *
  3316. superset_allocator (rules, val)
  3317.      struct rx_hash_rules * rules;
  3318.      void * val;
  3319. #endif
  3320. {
  3321.   struct rx_cache * cache
  3322.     = ((struct rx_cache *)
  3323.        ((char *)rules
  3324.     - (unsigned long)(&((struct rx_cache *)0)->superset_hash_rules)));
  3325.   struct rx_superset * template = (struct rx_superset *)val;
  3326.   struct rx_superset * newset
  3327.     = ((struct rx_superset *)
  3328.        rx_cache_malloc_or_get (cache,
  3329.                    &cache->free_supersets,
  3330.                    sizeof (*template)));
  3331.   newset->refs = 0;
  3332.   newset->car = template->car;
  3333.   newset->id = template->car->id;
  3334.   newset->cdr = template->cdr;
  3335.   newset->superstate = 0;
  3336.   rx_protect_superset (rx, template->cdr);
  3337.   newset->hash_item.data = (void *)newset;
  3338.   newset->hash_item.binding = 0;
  3339.   return &newset->hash_item;
  3340. }
  3341.  
  3342. #ifdef __STDC__
  3343. static struct rx_hash * 
  3344. super_hash_allocator (struct rx_hash_rules * rules)
  3345. #else
  3346. static struct rx_hash * 
  3347. super_hash_allocator (rules)
  3348.      struct rx_hash_rules * rules;
  3349. #endif
  3350. {
  3351.   struct rx_cache * cache
  3352.     = ((struct rx_cache *)
  3353.        ((char *)rules
  3354.     - (unsigned long)(&((struct rx_cache *)0)->superset_hash_rules)));
  3355.   return ((struct rx_hash *)
  3356.       rx_cache_malloc_or_get (cache,
  3357.                   &cache->free_hash, sizeof (struct rx_hash)));
  3358. }
  3359.  
  3360.  
  3361. #ifdef __STDC__
  3362. static void
  3363. super_hash_liberator (struct rx_hash * hash, struct rx_hash_rules * rules)
  3364. #else
  3365. static void
  3366. super_hash_liberator (hash, rules)
  3367.      struct rx_hash * hash;
  3368.      struct rx_hash_rules * rules;
  3369. #endif
  3370. {
  3371.   struct rx_cache * cache
  3372.     = ((struct rx_cache *)
  3373.        (char *)rules - (long)(&((struct rx_cache *)0)->superset_hash_rules));
  3374.   rx_cache_free (cache, &cache->free_hash, (char *)hash);
  3375. }
  3376.  
  3377. #ifdef __STDC__
  3378. static void
  3379. superset_hash_item_liberator (struct rx_hash_item * it,
  3380.                   struct rx_hash_rules * rules)
  3381. #else
  3382. static void
  3383. superset_hash_item_liberator (it, rules) /* Well, it does ya know. */
  3384.      struct rx_hash_item * it;
  3385.      struct rx_hash_rules * rules;
  3386. #endif
  3387. {
  3388. }
  3389.  
  3390. int rx_cache_bound = 128;
  3391. static int rx_default_cache_got = 0;
  3392.  
  3393. #ifdef __STDC__
  3394. static int
  3395. bytes_for_cache_size (int supers, int cset_size)
  3396. #else
  3397. static int
  3398. bytes_for_cache_size (supers, cset_size)
  3399.      int supers;
  3400.      int cset_size;
  3401. #endif
  3402. {
  3403.   return (int)
  3404.     ((float)supers *
  3405.      (  (1.03 * (float) (  rx_sizeof_bitset (cset_size)
  3406.              + sizeof (struct rx_super_edge)))
  3407.       + (1.80 * (float) sizeof (struct rx_possible_future))
  3408.       + (float) (  sizeof (struct rx_superstate)
  3409.          + cset_size * sizeof (struct rx_inx))));
  3410. }
  3411.  
  3412. #ifdef __STDC__
  3413. static void
  3414. rx_morecore (struct rx_cache * cache)
  3415. #else
  3416. static void
  3417. rx_morecore (cache)
  3418.      struct rx_cache * cache;
  3419. #endif
  3420. {
  3421.   if (rx_default_cache_got >= rx_cache_bound)
  3422.     return;
  3423.  
  3424.   rx_default_cache_got += 16;
  3425.   cache->superstates_allowed = rx_cache_bound;
  3426.   {
  3427.     struct rx_blocklist ** pos = &cache->memory;
  3428.     int size = bytes_for_cache_size (16, cache->local_cset_size);
  3429.     while (*pos)
  3430.       pos = &(*pos)->next;
  3431.     *pos = ((struct rx_blocklist *)
  3432.         malloc (size + sizeof (struct rx_blocklist))); 
  3433.     if (!*pos)
  3434.       return;
  3435.  
  3436.     (*pos)->next = 0;
  3437.     (*pos)->bytes = size;
  3438.     cache->memory_pos = *pos;
  3439.     cache->memory_addr = (char *)*pos + sizeof (**pos);
  3440.     cache->bytes_left = size;
  3441.   }
  3442. }
  3443.  
  3444. static struct rx_cache default_cache = 
  3445. {
  3446.   {
  3447.     supersetcmp,
  3448.     super_hash_allocator,
  3449.     super_hash_liberator,
  3450.     superset_allocator,
  3451.     superset_hash_item_liberator,
  3452.   },
  3453.   0,
  3454.   0,
  3455.   0,
  3456.   0,
  3457.   rx_morecore,
  3458.  
  3459.   0,
  3460.   0,
  3461.   0,
  3462.   0,
  3463.   0,
  3464.  
  3465.   0,
  3466.   0,
  3467.  
  3468.   0,
  3469.  
  3470.   0,
  3471.   0,
  3472.   0,
  3473.   0,
  3474.   128,
  3475.  
  3476.   256,
  3477.   rx_id_instruction_table,
  3478.  
  3479.   {
  3480.     0,
  3481.     0,
  3482.     {0},
  3483.     {0},
  3484.     {0}
  3485.   }
  3486. };
  3487.  
  3488. /* This adds an element to a superstate set.  These sets are lists, such
  3489.  * that lists with == elements are ==.  The empty set is returned by
  3490.  * superset_cons (rx, 0, 0) and is NOT equivelent to 
  3491.  * (struct rx_superset)0.
  3492.  */
  3493.  
  3494. #ifdef __STDC__
  3495. RX_DECL struct rx_superset *
  3496. rx_superset_cons (struct rx * rx,
  3497.           struct rx_nfa_state *car, struct rx_superset *cdr)
  3498. #else
  3499. RX_DECL struct rx_superset *
  3500. rx_superset_cons (rx, car, cdr)
  3501.      struct rx * rx;
  3502.      struct rx_nfa_state *car;
  3503.      struct rx_superset *cdr;
  3504. #endif
  3505. {
  3506.   struct rx_cache * cache = rx->cache;
  3507.   if (!car && !cdr)
  3508.     {
  3509.       if (!cache->empty_superset)
  3510.     {
  3511.       cache->empty_superset
  3512.         = ((struct rx_superset *)
  3513.            rx_cache_malloc_or_get (cache, &cache->free_supersets,
  3514.                        sizeof (struct rx_superset)));
  3515.       if (!cache->empty_superset)
  3516.         return 0;
  3517.       bzero (cache->empty_superset, sizeof (struct rx_superset));
  3518.       cache->empty_superset->refs = 1000;
  3519.     }
  3520.       return cache->empty_superset;
  3521.     }
  3522.   {
  3523.     struct rx_superset template;
  3524.     struct rx_hash_item * hit;
  3525.     template.car = car;
  3526.     template.cdr = cdr;
  3527.     template.id = car->id;
  3528.     hit = rx_hash_store (&cache->superset_table,
  3529.              (unsigned long)car ^ car->id ^ (unsigned long)cdr,
  3530.              (void *)&template,
  3531.              &cache->superset_hash_rules);
  3532.     return (hit
  3533.         ?  (struct rx_superset *)hit->data
  3534.         : 0);
  3535.   }
  3536. }
  3537.  
  3538. /* This computes a union of two NFA state sets.  The sets do not have the
  3539.  * same representation though.  One is a RX_SUPERSET structure (part
  3540.  * of the superstate NFA) and the other is an NFA_STATE_SET (part of the NFA).
  3541.  */
  3542.  
  3543. #ifdef __STDC__
  3544. RX_DECL struct rx_superset *
  3545. rx_superstate_eclosure_union
  3546.   (struct rx * rx, struct rx_superset *set, struct rx_nfa_state_set *ecl) 
  3547. #else
  3548. RX_DECL struct rx_superset *
  3549. rx_superstate_eclosure_union (rx, set, ecl)
  3550.      struct rx * rx;
  3551.      struct rx_superset *set;
  3552.      struct rx_nfa_state_set *ecl;
  3553. #endif
  3554. {
  3555.   if (!ecl)
  3556.     return set;
  3557.   if (!set->car)
  3558.     return rx_superset_cons (rx, ecl->car,
  3559.                 rx_superstate_eclosure_union (rx, set, ecl->cdr));
  3560.   {
  3561.     if (set->car == ecl->car)
  3562.       return rx_superstate_eclosure_union (rx, set, ecl->cdr);
  3563.     if (set->car > ecl->car)
  3564.       return rx_superset_cons (rx, set->car,
  3565.                   rx_superstate_eclosure_union (rx,
  3566.                                 set->cdr, ecl));
  3567.     return rx_superset_cons (rx, ecl->car,
  3568.                 rx_superstate_eclosure_union (rx, set, ecl->cdr));
  3569.   }
  3570. }
  3571.  
  3572.  
  3573. #ifdef __STDC__
  3574. RX_DECL void 
  3575. rx_release_superset (struct rx *rx,
  3576.              struct rx_superset *set)
  3577. #else
  3578. RX_DECL void 
  3579. rx_release_superset (rx, set)
  3580.      struct rx *rx;
  3581.      struct rx_superset *set;
  3582. #endif
  3583. {
  3584.   release_superset_low (rx->cache, set);
  3585. }
  3586.  
  3587.  
  3588.  
  3589. /*
  3590.  * This makes sure that a list of rx_distinct_futures contains
  3591.  * a future for each possible set of side effects in the eclosure
  3592.  * of a given state.  This is some of the work of filling in a
  3593.  * superstate transition. 
  3594.  */
  3595.  
  3596. #ifdef __STDC__
  3597. static struct rx_distinct_future *
  3598. include_futures (struct rx *rx,
  3599.          struct rx_distinct_future *df, struct rx_nfa_state
  3600.          *state, struct rx_superstate *superstate) 
  3601. #else
  3602. static struct rx_distinct_future *
  3603. include_futures (rx, df, state, superstate)
  3604.      struct rx *rx;
  3605.      struct rx_distinct_future *df;
  3606.      struct rx_nfa_state *state;
  3607.      struct rx_superstate *superstate;
  3608. #endif
  3609. {
  3610.   struct rx_possible_future *future;
  3611.   struct rx_cache * cache = rx->cache;
  3612.   for (future = state->futures; future; future = future->next)
  3613.     {
  3614.       struct rx_distinct_future *dfp;
  3615.       for (dfp = df; dfp; dfp = dfp->next_same_super_edge)
  3616.     if (dfp->effects == future->effects)
  3617.       break;
  3618.       if (!dfp)
  3619.     {
  3620.       dfp
  3621.         = ((struct rx_distinct_future *)
  3622.            rx_cache_malloc_or_get (cache, &cache->free_discernable_futures,
  3623.                        sizeof (struct rx_distinct_future)));
  3624.       if (!dfp)
  3625.         return 0;
  3626.       dfp->next_same_super_edge = df;
  3627.       df = dfp;
  3628.       dfp->next_same_dest = dfp->prev_same_dest = dfp;
  3629.       dfp->future = 0;
  3630.       dfp->present = superstate;
  3631.       dfp->future_frame.inx = rx->instruction_table[rx_cache_miss];
  3632.       dfp->future_frame.data = 0;
  3633.       dfp->future_frame.data_2 = (void *) dfp;
  3634.       dfp->side_effects_frame.inx
  3635.         = rx->instruction_table[rx_do_side_effects];
  3636.       dfp->side_effects_frame.data = 0;
  3637.       dfp->side_effects_frame.data_2 = (void *) dfp;
  3638.       dfp->effects = future->effects;
  3639.     }
  3640.     }
  3641.   return df;
  3642. }
  3643.  
  3644.  
  3645.  
  3646.  
  3647. /* This constructs a new superstate from its state set.  The only 
  3648.  * complexity here is memory management.
  3649.  */
  3650. #ifdef __STDC__
  3651. RX_DECL struct rx_superstate *
  3652. rx_superstate (struct rx *rx,
  3653.            struct rx_superset *set)
  3654. #else
  3655. RX_DECL struct rx_superstate *
  3656. rx_superstate (rx, set)
  3657.      struct rx *rx;
  3658.      struct rx_superset *set;
  3659. #endif
  3660. {
  3661.   struct rx_cache * cache = rx->cache;
  3662.   struct rx_superstate * superstate = 0;
  3663.  
  3664.   /* Does the superstate already exist in the cache? */
  3665.   if (set->superstate)
  3666.     {
  3667.       if (set->superstate->rx_id != rx->rx_id)
  3668.     {
  3669.       /* Aha.  It is in the cache, but belongs to a superstate
  3670.        * that refers to an NFA that no longer exists.
  3671.        * (We know it no longer exists because it was evidently
  3672.        *  stored in the same region of memory as the current nfa
  3673.        *  yet it has a different id.)
  3674.        */
  3675.       superstate = set->superstate;
  3676.       if (!superstate->is_semifree)
  3677.         {
  3678.           if (cache->lru_superstate == superstate)
  3679.         {
  3680.           cache->lru_superstate = superstate->next_recyclable;
  3681.           if (cache->lru_superstate == superstate)
  3682.             cache->lru_superstate = 0;
  3683.         }
  3684.           {
  3685.         superstate->next_recyclable->prev_recyclable
  3686.           = superstate->prev_recyclable;
  3687.         superstate->prev_recyclable->next_recyclable
  3688.           = superstate->next_recyclable;
  3689.         if (!cache->semifree_superstate)
  3690.           {
  3691.             (cache->semifree_superstate
  3692.              = superstate->next_recyclable
  3693.              = superstate->prev_recyclable
  3694.              = superstate);
  3695.           }
  3696.         else
  3697.           {
  3698.             superstate->next_recyclable = cache->semifree_superstate;
  3699.             superstate->prev_recyclable
  3700.               = cache->semifree_superstate->prev_recyclable;
  3701.             superstate->next_recyclable->prev_recyclable
  3702.               = superstate;
  3703.             superstate->prev_recyclable->next_recyclable
  3704.               = superstate;
  3705.             cache->semifree_superstate = superstate;
  3706.           }
  3707.         ++cache->semifree_superstates;
  3708.           }
  3709.         }
  3710.       set->superstate = 0;
  3711.       goto handle_cache_miss;
  3712.     }
  3713.       ++cache->hits;
  3714.       superstate = set->superstate;
  3715.  
  3716.       rx_refresh_this_superstate (cache, superstate);
  3717.       return superstate;
  3718.     }
  3719.  
  3720.  handle_cache_miss:
  3721.  
  3722.   /* This point reached only for cache misses. */
  3723.   ++cache->misses;
  3724. #if RX_DEBUG
  3725.   if (rx_debug_trace > 1)
  3726.     {
  3727.       struct rx_superset * setp = set;
  3728.       fprintf (stderr, "Building a superstet %d(%d): ", rx->rx_id, set);
  3729.       while (setp)
  3730.     {
  3731.       fprintf (stderr, "%d ", setp->id);
  3732.       setp = setp->cdr;
  3733.     }
  3734.       fprintf (stderr, "(%d)\n", set);
  3735.     }
  3736. #endif
  3737.   superstate = (struct rx_superstate *)rx_cache_get_superstate (cache);
  3738.   if (!superstate)
  3739.     return 0;
  3740.  
  3741.   if (!cache->lru_superstate)
  3742.     (cache->lru_superstate
  3743.      = superstate->next_recyclable
  3744.      = superstate->prev_recyclable
  3745.      = superstate);
  3746.   else
  3747.     {
  3748.       superstate->next_recyclable = cache->lru_superstate;
  3749.       superstate->prev_recyclable = cache->lru_superstate->prev_recyclable;
  3750.       (  superstate->prev_recyclable->next_recyclable
  3751.        = superstate->next_recyclable->prev_recyclable
  3752.        = superstate);
  3753.     }
  3754.   superstate->rx_id = rx->rx_id;
  3755.   superstate->transition_refs = 0;
  3756.   superstate->locks = 0;
  3757.   superstate->is_semifree = 0;
  3758.   set->superstate = superstate;
  3759.   superstate->contents = set;
  3760.   rx_protect_superset (rx, set);
  3761.   superstate->edges = 0;
  3762.   {
  3763.     int x;
  3764.     /* None of the transitions from this superstate are known yet. */
  3765.     for (x = 0; x < rx->local_cset_size; ++x) /* &&&&& 3.8 % */
  3766.       {
  3767.     struct rx_inx * ifr = &superstate->transitions[x];
  3768.     ifr->inx = rx->instruction_table [rx_cache_miss];
  3769.     ifr->data = ifr->data_2 = 0;
  3770.       }
  3771.   }
  3772.   return superstate;
  3773. }
  3774.  
  3775.  
  3776. /* This computes the destination set of one edge of the superstate NFA.
  3777.  * Note that a RX_DISTINCT_FUTURE is a superstate edge.
  3778.  * Returns 0 on an allocation failure.
  3779.  */
  3780.  
  3781. #ifdef __STDC__
  3782. static int 
  3783. solve_destination (struct rx *rx, struct rx_distinct_future *df)
  3784. #else
  3785. static int 
  3786. solve_destination (rx, df)
  3787.      struct rx *rx;
  3788.      struct rx_distinct_future *df;
  3789. #endif
  3790. {
  3791.   struct rx_super_edge *tc = df->edge;
  3792.   struct rx_superset *nfa_state;
  3793.   struct rx_superset *nil_set = rx_superset_cons (rx, 0, 0);
  3794.   struct rx_superset *solution = nil_set;
  3795.   struct rx_superstate *dest;
  3796.  
  3797.   /* Iterate over all NFA states in the state set of this superstate. */
  3798.   for (nfa_state = df->present->contents;
  3799.        nfa_state->car;
  3800.        nfa_state = nfa_state->cdr)
  3801.     {
  3802.       struct rx_nfa_edge *e;
  3803.       /* Iterate over all edges of each NFA state. */
  3804.       for (e = nfa_state->car->edges; e; e = e->next)
  3805.         /* If we find an edge that is labeled with 
  3806.      * the characters we are solving for.....
  3807.      */
  3808.     if (rx_bitset_is_subset (rx->local_cset_size,
  3809.                  tc->cset, e->params.cset))
  3810.       {
  3811.         struct rx_nfa_state *n = e->dest;
  3812.         struct rx_possible_future *pf;
  3813.         /* ....search the partial epsilon closures of the destination
  3814.          * of that edge for a path that involves the same set of
  3815.          * side effects we are solving for.
  3816.          * If we find such a RX_POSSIBLE_FUTURE, we add members to the
  3817.          * stateset we are computing.
  3818.          */
  3819.         for (pf = n->futures; pf; pf = pf->next)
  3820.           if (pf->effects == df->effects)
  3821.         {
  3822.           struct rx_superset * old_sol = solution;
  3823.           rx_protect_superset (rx, solution);
  3824.           solution =
  3825.             rx_superstate_eclosure_union (rx, solution, pf->destset);
  3826.           if (!solution)
  3827.             return 0;
  3828.           rx_release_superset (rx, old_sol);
  3829.         }
  3830.       }
  3831.     }
  3832.   /* It is possible that the RX_DISTINCT_FUTURE we are working on has 
  3833.    * the empty set of NFA states as its definition.  In that case, this
  3834.    * is a failure point.
  3835.    */
  3836.   if (solution == nil_set)
  3837.     {
  3838.       df->future_frame.inx = (void *) rx_backtrack;
  3839.       df->future_frame.data = 0;
  3840.       df->future_frame.data_2 = 0;
  3841.       return 1;
  3842.     }
  3843.   rx_protect_superset (rx, solution);
  3844.   dest = rx_superstate (rx, solution);
  3845.   rx_release_superset (rx, solution);
  3846.   if (!dest)
  3847.     return 0;
  3848.  
  3849.   {
  3850.     struct rx_distinct_future *dft;
  3851.     dft = df;
  3852.     df->prev_same_dest->next_same_dest = 0;
  3853.     while (dft)
  3854.       {
  3855.     dft->future = dest;
  3856.     dft->future_frame.inx = rx->instruction_table[rx_next_char];
  3857.     dft->future_frame.data = (void *) dest->transitions;
  3858.     dft = dft->next_same_dest;
  3859.       }
  3860.     df->prev_same_dest->next_same_dest = df;
  3861.   }
  3862.   if (!dest->transition_refs)
  3863.     dest->transition_refs = df;
  3864.   else
  3865.     {
  3866.       struct rx_distinct_future *dft = dest->transition_refs->next_same_dest;
  3867.       dest->transition_refs->next_same_dest = df->next_same_dest;
  3868.       df->next_same_dest->prev_same_dest = dest->transition_refs;
  3869.       df->next_same_dest = dft;
  3870.       dft->prev_same_dest = df;
  3871.     }
  3872.   return 1;
  3873. }
  3874.  
  3875.  
  3876. /* This takes a superstate and a character, and computes some edges
  3877.  * from the superstate NFA.  In particular, this computes all edges
  3878.  * that lead from SUPERSTATE given CHR.   This function also 
  3879.  * computes the set of characters that share this edge set.
  3880.  * This returns 0 on allocation error.
  3881.  * The character set and list of edges are returned through 
  3882.  * the paramters CSETOUT and DFOUT.
  3883. } */
  3884.  
  3885. #ifdef __STDC__
  3886. static int 
  3887. compute_super_edge (struct rx *rx, struct rx_distinct_future **dfout,
  3888.               rx_Bitset csetout, struct rx_superstate *superstate,
  3889.               unsigned char chr)  
  3890. #else
  3891. static int 
  3892. compute_super_edge (rx, dfout, csetout, superstate, chr)
  3893.      struct rx *rx;
  3894.      struct rx_distinct_future **dfout;
  3895.      rx_Bitset csetout;
  3896.      struct rx_superstate *superstate;
  3897.      unsigned char chr;
  3898. #endif
  3899. {
  3900.   struct rx_superset *stateset = superstate->contents;
  3901.  
  3902.   /* To compute the set of characters that share edges with CHR, 
  3903.    * we start with the full character set, and subtract.
  3904.    */
  3905.   rx_bitset_universe (rx->local_cset_size, csetout);
  3906.   *dfout = 0;
  3907.  
  3908.   /* Iterate over the NFA states in the superstate state-set. */
  3909.   while (stateset->car)
  3910.     {
  3911.       struct rx_nfa_edge *e;
  3912.       for (e = stateset->car->edges; e; e = e->next)
  3913.     if (RX_bitset_member (e->params.cset, chr))
  3914.       {
  3915.         /* If we find an NFA edge that applies, we make sure there
  3916.          * are corresponding edges in the superstate NFA.
  3917.          */
  3918.         *dfout = include_futures (rx, *dfout, e->dest, superstate);
  3919.         if (!*dfout)
  3920.           return 0;
  3921.         /* We also trim the character set a bit. */
  3922.         rx_bitset_intersection (rx->local_cset_size,
  3923.                     csetout, e->params.cset);
  3924.       }
  3925.     else
  3926.       /* An edge that doesn't apply at least tells us some characters
  3927.        * that don't share the same edge set as CHR.
  3928.        */
  3929.       rx_bitset_difference (rx->local_cset_size, csetout, e->params.cset);
  3930.       stateset = stateset->cdr;
  3931.     }
  3932.   return 1;
  3933. }
  3934.  
  3935.  
  3936. /* This is a constructor for RX_SUPER_EDGE structures.  These are
  3937.  * wrappers for lists of superstate NFA edges that share character sets labels.
  3938.  * If a transition class contains more than one rx_distinct_future (superstate
  3939.  * edge), then it represents a non-determinism in the superstate NFA.
  3940.  */
  3941.  
  3942. #ifdef __STDC__
  3943. static struct rx_super_edge *
  3944. rx_super_edge (struct rx *rx,
  3945.            struct rx_superstate *super, rx_Bitset cset,
  3946.            struct rx_distinct_future *df) 
  3947. #else
  3948. static struct rx_super_edge *
  3949. rx_super_edge (rx, super, cset, df)
  3950.      struct rx *rx;
  3951.      struct rx_superstate *super;
  3952.      rx_Bitset cset;
  3953.      struct rx_distinct_future *df;
  3954. #endif
  3955. {
  3956.   struct rx_super_edge *tc =
  3957.     (struct rx_super_edge *)rx_cache_malloc_or_get
  3958.       (rx->cache, &rx->cache->free_transition_classes,
  3959.        sizeof (struct rx_super_edge) + rx_sizeof_bitset (rx->local_cset_size));
  3960.  
  3961.   if (!tc)
  3962.     return 0;
  3963.   tc->next = super->edges;
  3964.   super->edges = tc;
  3965.   tc->rx_backtrack_frame.inx = rx->instruction_table[rx_backtrack_point];
  3966.   tc->rx_backtrack_frame.data = 0;
  3967.   tc->rx_backtrack_frame.data_2 = (void *) tc;
  3968.   tc->options = df;
  3969.   tc->cset = (rx_Bitset) ((char *) tc + sizeof (*tc));
  3970.   rx_bitset_assign (rx->local_cset_size, tc->cset, cset);
  3971.   while (df)
  3972.     {
  3973.       df->edge = tc;
  3974.       df = df->next_same_super_edge;
  3975.     }
  3976.   return tc;
  3977. }
  3978.  
  3979.  
  3980. /* There are three kinds of cache miss.  The first occurs when a
  3981.  * transition is taken that has never been computed during the
  3982.  * lifetime of the source superstate.  That cache miss is handled by
  3983.  * calling COMPUTE_SUPER_EDGE.  The second kind of cache miss
  3984.  * occurs when the destination superstate of a transition doesn't
  3985.  * exist.  SOLVE_DESTINATION is used to construct the destination superstate.
  3986.  * Finally, the third kind of cache miss occurs when the destination
  3987.  * superstate of a transition is in a `semi-free state'.  That case is
  3988.  * handled by UNFREE_SUPERSTATE.
  3989.  *
  3990.  * The function of HANDLE_CACHE_MISS is to figure out which of these
  3991.  * cases applies.
  3992.  */
  3993.  
  3994. #ifdef __STDC__
  3995. static void
  3996. install_partial_transition  (struct rx_superstate *super,
  3997.                  struct rx_inx *answer,
  3998.                  RX_subset set, int offset)
  3999. #else
  4000. static void
  4001. install_partial_transition  (super, answer, set, offset)
  4002.      struct rx_superstate *super;
  4003.      struct rx_inx *answer;
  4004.      RX_subset set;
  4005.      int offset;
  4006. #endif
  4007. {
  4008.   int start = offset;
  4009.   int end = start + 32;
  4010.   RX_subset pos = 1;
  4011.   struct rx_inx * transitions = super->transitions;
  4012.   
  4013.   while (start < end)
  4014.     {
  4015.       if (set & pos)
  4016.     transitions[start] = *answer;
  4017.       pos <<= 1;
  4018.       ++start;
  4019.     }
  4020. }
  4021.  
  4022.  
  4023. #ifdef __STDC__
  4024. RX_DECL struct rx_inx *
  4025. rx_handle_cache_miss
  4026.   (struct rx *rx, struct rx_superstate *super, unsigned char chr, void *data) 
  4027. #else
  4028. RX_DECL struct rx_inx *
  4029. rx_handle_cache_miss (rx, super, chr, data)
  4030.      struct rx *rx;
  4031.      struct rx_superstate *super;
  4032.      unsigned char chr;
  4033.      void *data;
  4034. #endif
  4035. {
  4036.   int offset = chr / RX_subset_bits;
  4037.   struct rx_distinct_future *df = data;
  4038.  
  4039.   if (!df)            /* must be the shared_cache_miss_frame */
  4040.     {
  4041.       /* Perhaps this is just a transition waiting to be filled. */
  4042.       struct rx_super_edge *tc;
  4043.       RX_subset mask = rx_subset_singletons [chr % RX_subset_bits];
  4044.  
  4045.       for (tc = super->edges; tc; tc = tc->next)
  4046.     if (tc->cset[offset] & mask)
  4047.       {
  4048.         struct rx_inx * answer;
  4049.         df = tc->options;
  4050.         answer = (tc->options->next_same_super_edge
  4051.               ? &tc->rx_backtrack_frame
  4052.               : (df->effects
  4053.              ? &df->side_effects_frame
  4054.              : &df->future_frame));
  4055.         install_partial_transition (super, answer,
  4056.                     tc->cset [offset], offset * 32);
  4057.         return answer;
  4058.       }
  4059.       /* Otherwise, it's a flushed or  newly encountered edge. */
  4060.       {
  4061.     rx_Bitset trcset = rx_cset (rx);
  4062.     struct rx_inx *answer;
  4063.     if (!trcset)
  4064.       return 0;
  4065.     rx_lock_superstate (rx, super);
  4066.     if (!compute_super_edge (rx, &df, trcset, super, chr))
  4067.       {
  4068.         rx_unlock_superstate (rx, super);
  4069.         return 0;
  4070.       }
  4071.     if (!df)        /* We just computed the fail transition. */
  4072.       {
  4073.         static struct rx_inx
  4074.           shared_fail_frame = { (void *)rx_backtrack, 0, 0 };
  4075.         answer = &shared_fail_frame;
  4076.       }
  4077.     else
  4078.       {
  4079.         tc = rx_super_edge (rx, super, trcset, df);
  4080.         if (!tc)
  4081.           {
  4082.         rx_unlock_superstate (rx, super);
  4083.         return 0;
  4084.           }
  4085.         answer = (tc->options->next_same_super_edge
  4086.               ? &tc->rx_backtrack_frame
  4087.               : (df->effects
  4088.              ? &df->side_effects_frame
  4089.              : &df->future_frame));
  4090.       }
  4091.     install_partial_transition (super, answer,
  4092.                     trcset[offset], offset * 32);
  4093.     rx_free_cset (rx, trcset);
  4094.     rx_unlock_superstate (rx, super);
  4095.     return answer;
  4096.       }
  4097.     }
  4098.   else if (df->future) /* A cache miss on an edge with a future? Must be
  4099.             * a semi-free destination. */
  4100.     {                
  4101.       if (df->future->is_semifree)
  4102.     refresh_semifree_superstate (rx->cache, df->future);
  4103.       return &df->future_frame;
  4104.     }
  4105.   else
  4106.     /* no future superstate on an existing edge */
  4107.     {
  4108.       rx_lock_superstate (rx, super);
  4109.       if (!solve_destination (rx, df))
  4110.     {
  4111.       rx_unlock_superstate (rx, super);
  4112.       return 0;
  4113.     }
  4114.       if (!(df->effects || df->edge->options->next_same_super_edge))
  4115.     install_partial_transition (super, &df->future_frame,
  4116.                     df->edge->cset[offset], offset * 32);
  4117.       rx_unlock_superstate (rx, super);
  4118.       return &df->future_frame;
  4119.     }
  4120. }
  4121.  
  4122.  
  4123.  
  4124.  
  4125. /* The rest of the code provides a regex.c compatable interface. */
  4126.  
  4127.  
  4128. const char *re_error_msg[] =
  4129. {
  4130.   0,                        /* REG_NOUT */
  4131.   "No match",                    /* REG_NOMATCH */
  4132.   "Invalid regular expression",            /* REG_BADPAT */
  4133.   "Invalid collation character",        /* REG_ECOLLATE */
  4134.   "Invalid character class name",        /* REG_ECTYPE */
  4135.   "Trailing backslash",                /* REG_EESCAPE */
  4136.   "Invalid back reference",            /* REG_ESUBREG */
  4137.   "Unmatched [ or [^",                /* REG_EBRACK */
  4138.   "Unmatched ( or \\(",                /* REG_EPAREN */
  4139.   "Unmatched \\{",                /* REG_EBRACE */
  4140.   "Invalid content of \\{\\}",            /* REG_BADBR */
  4141.   "Invalid range end",                /* REG_ERANGE */
  4142.   "Memory exhausted",                /* REG_ESPACE */
  4143.   "Invalid preceding regular expression",    /* REG_BADRPT */
  4144.   "Premature end of regular expression",    /* REG_EEND */
  4145.   "Regular expression too big",            /* REG_ESIZE */
  4146.   "Unmatched ) or \\)",                /* REG_ERPAREN */
  4147. };
  4148.  
  4149.  
  4150.  
  4151. /* 
  4152.  * Macros used while compiling patterns.
  4153.  *
  4154.  * By convention, PEND points just past the end of the uncompiled pattern,
  4155.  * P points to the read position in the pattern.  `translate' is the name
  4156.  * of the translation table (`TRANSLATE' is the name of a macro that looks
  4157.  * things up in `translate').
  4158.  */
  4159.  
  4160.  
  4161. /*
  4162.  * Fetch the next character in the uncompiled pattern---translating it 
  4163.  * if necessary. *Also cast from a signed character in the constant
  4164.  * string passed to us by the user to an unsigned char that we can use
  4165.  * as an array index (in, e.g., `translate').
  4166.  */
  4167. #define PATFETCH(c)                            \
  4168.  do {if (p == pend) return REG_EEND;                    \
  4169.     c = (unsigned char) *p++;                        \
  4170.     c = translate[c];                             \
  4171.  } while (0)
  4172.  
  4173. /* 
  4174.  * Fetch the next character in the uncompiled pattern, with no
  4175.  * translation.
  4176.  */
  4177. #define PATFETCH_RAW(c)                            \
  4178.   do {if (p == pend) return REG_EEND;                    \
  4179.     c = (unsigned char) *p++;                         \
  4180.   } while (0)
  4181.  
  4182. /* Go backwards one character in the pattern.  */
  4183. #define PATUNFETCH p--
  4184.  
  4185.  
  4186. #define TRANSLATE(d) translate[(unsigned char) (d)]
  4187.  
  4188. typedef unsigned regnum_t;
  4189.  
  4190. /* Since offsets can go either forwards or backwards, this type needs to
  4191.  * be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1.
  4192.  */
  4193. typedef int pattern_offset_t;
  4194.  
  4195. typedef struct
  4196. {
  4197.   struct rexp_node ** top_expression; /* was begalt */
  4198.   struct rexp_node ** last_expression; /* was laststart */
  4199.   pattern_offset_t inner_group_offset;
  4200.   regnum_t regnum;
  4201. } compile_stack_elt_t;
  4202.  
  4203. typedef struct
  4204. {
  4205.   compile_stack_elt_t *stack;
  4206.   unsigned size;
  4207.   unsigned avail;            /* Offset of next open position.  */
  4208. } compile_stack_type;
  4209.  
  4210.  
  4211. #define INIT_COMPILE_STACK_SIZE 32
  4212.  
  4213. #define COMPILE_STACK_EMPTY  (compile_stack.avail == 0)
  4214. #define COMPILE_STACK_FULL  (compile_stack.avail == compile_stack.size)
  4215.  
  4216. /* The next available element.  */
  4217. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  4218.  
  4219.  
  4220. /* Set the bit for character C in a list.  */
  4221. #define SET_LIST_BIT(c)                               \
  4222.   (b[((unsigned char) (c)) / BYTEWIDTH]               \
  4223.    |= 1 << (((unsigned char) c) % BYTEWIDTH))
  4224.  
  4225. /* Get the next unsigned number in the uncompiled pattern.  */
  4226. #define GET_UNSIGNED_NUMBER(num)                     \
  4227.   { if (p != pend)                            \
  4228.      {                                    \
  4229.        PATFETCH (c);                             \
  4230.        while (isdigit (c))                         \
  4231.          {                                 \
  4232.            if (num < 0)                            \
  4233.               num = 0;                            \
  4234.            num = num * 10 + c - '0';                     \
  4235.            if (p == pend)                         \
  4236.               break;                             \
  4237.            PATFETCH (c);                        \
  4238.          }                                 \
  4239.        }                                 \
  4240.     }        
  4241.  
  4242. #define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
  4243.  
  4244. #define IS_CHAR_CLASS(string)                        \
  4245.    (!strcmp (string, "alpha") || !strcmp (string, "upper")        \
  4246.     || !strcmp (string, "lower") || !strcmp (string, "digit")        \
  4247.     || !strcmp (string, "alnum") || !strcmp (string, "xdigit")        \
  4248.     || !strcmp (string, "space") || !strcmp (string, "print")        \
  4249.     || !strcmp (string, "punct") || !strcmp (string, "graph")        \
  4250.     || !strcmp (string, "cntrl") || !strcmp (string, "blank"))
  4251.  
  4252.  
  4253. /* These predicates are used in regex_compile. */
  4254.  
  4255. /* P points to just after a ^ in PATTERN.  Return true if that ^ comes
  4256.  * after an alternative or a begin-subexpression.  We assume there is at
  4257.  * least one character before the ^.  
  4258.  */
  4259.  
  4260. #ifdef __STDC__
  4261. static boolean
  4262. at_begline_loc_p (const char *pattern, const char * p, reg_syntax_t syntax)
  4263. #else
  4264. static boolean
  4265. at_begline_loc_p (pattern, p, syntax)
  4266.      const char *pattern;
  4267.      const char * p;
  4268.      reg_syntax_t syntax;
  4269. #endif
  4270. {
  4271.   const char *prev = p - 2;
  4272.   boolean prev_prev_backslash = ((prev > pattern) && (prev[-1] == '\\'));
  4273.   
  4274.     return
  4275.       
  4276.       (/* After a subexpression?  */
  4277.        ((*prev == '(') && ((syntax & RE_NO_BK_PARENS) || prev_prev_backslash))
  4278.        ||
  4279.        /* After an alternative?  */
  4280.        ((*prev == '|') && ((syntax & RE_NO_BK_VBAR) || prev_prev_backslash))
  4281.        );
  4282. }
  4283.  
  4284. /* The dual of at_begline_loc_p.  This one is for $.  We assume there is
  4285.  * at least one character after the $, i.e., `P < PEND'.
  4286.  */
  4287.  
  4288. #ifdef __STDC__
  4289. static boolean
  4290. at_endline_loc_p (const char *p, const char *pend, int syntax)
  4291. #else
  4292. static boolean
  4293. at_endline_loc_p (p, pend, syntax)
  4294.      const char *p;
  4295.      const char *pend;
  4296.      int syntax;
  4297. #endif
  4298. {
  4299.   const char *next = p;
  4300.   boolean next_backslash = (*next == '\\');
  4301.   const char *next_next = (p + 1 < pend) ? (p + 1) : 0;
  4302.   
  4303.   return
  4304.     (
  4305.      /* Before a subexpression?  */
  4306.      ((syntax & RE_NO_BK_PARENS)
  4307.       ? (*next == ')')
  4308.       : (next_backslash && next_next && (*next_next == ')')))
  4309.     ||
  4310.      /* Before an alternative?  */
  4311.      ((syntax & RE_NO_BK_VBAR)
  4312.       ? (*next == '|')
  4313.       : (next_backslash && next_next && (*next_next == '|')))
  4314.      );
  4315. }
  4316.  
  4317.  
  4318. static char id_translation[256] =
  4319. {
  4320.   0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
  4321.  10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  4322.  20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  4323.  30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  4324.  40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  4325.  50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  4326.  60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  4327.  70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  4328.  80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  4329.  90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  4330.  
  4331.  100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
  4332.  110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
  4333.  120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
  4334.  130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
  4335.  140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
  4336.  150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  4337.  160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
  4338.  170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
  4339.  180, 181, 182, 183, 184, 185, 186, 187, 188, 189,
  4340.  190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
  4341.  
  4342.  200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
  4343.  210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
  4344.  220, 221, 222, 223, 224, 225, 226, 227, 228, 229,
  4345.  230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  4346.  240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
  4347.  250, 251, 252, 253, 254, 255
  4348. };
  4349.  
  4350. /* The compiler keeps an inverted translation table.
  4351.  * This looks up/inititalize elements.
  4352.  * VALID is an array of booleans that validate CACHE.
  4353.  */
  4354.  
  4355. #ifdef __STDC__
  4356. static rx_Bitset
  4357. inverse_translation (struct re_pattern_buffer * rxb,
  4358.              char * valid, rx_Bitset cache,
  4359.              char * translate, int c)
  4360. #else
  4361. static rx_Bitset
  4362. inverse_translation (rxb, valid, cache, translate, c)
  4363.      struct re_pattern_buffer * rxb;
  4364.      char * valid;
  4365.      rx_Bitset cache;
  4366.      char * translate;
  4367.      int c;
  4368. #endif
  4369. {
  4370.   rx_Bitset cs
  4371.     = cache + c * rx_bitset_numb_subsets (rxb->rx.local_cset_size); 
  4372.  
  4373.   if (!valid[c])
  4374.     {
  4375.       int x;
  4376.       int c_tr = TRANSLATE(c);
  4377.       rx_bitset_null (rxb->rx.local_cset_size, cs);
  4378.       for (x = 0; x < 256; ++x)    /* &&&& 13.37 */
  4379.     if (TRANSLATE(x) == c_tr)
  4380.       RX_bitset_enjoin (cs, x);
  4381.       valid[c] = 1;
  4382.     }
  4383.   return cs;
  4384. }
  4385.  
  4386.  
  4387.  
  4388.  
  4389. /* More subroutine declarations and macros for regex_compile.  */
  4390.  
  4391. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and 
  4392.    false if it's not.  */
  4393.  
  4394. #ifdef __STDC__
  4395. static boolean
  4396. group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
  4397. #else
  4398. static boolean
  4399. group_in_compile_stack (compile_stack, regnum)
  4400.     compile_stack_type compile_stack;
  4401.     regnum_t regnum;
  4402. #endif
  4403. {
  4404.   int this_element;
  4405.  
  4406.   for (this_element = compile_stack.avail - 1;  
  4407.        this_element >= 0; 
  4408.        this_element--)
  4409.     if (compile_stack.stack[this_element].regnum == regnum)
  4410.       return true;
  4411.  
  4412.   return false;
  4413. }
  4414.  
  4415.  
  4416. /*
  4417.  * Read the ending character of a range (in a bracket expression) from the
  4418.  * uncompiled pattern *P_PTR (which ends at PEND).  We assume the
  4419.  * starting character is in `P[-2]'.  (`P[-1]' is the character `-'.)
  4420.  * Then we set the translation of all bits between the starting and
  4421.  * ending characters (inclusive) in the compiled pattern B.
  4422.  * 
  4423.  * Return an error code.
  4424.  * 
  4425.  * We use these short variable names so we can use the same macros as
  4426.  * `regex_compile' itself.  
  4427.  */
  4428.  
  4429. #ifdef __STDC__
  4430. static reg_errcode_t
  4431. compile_range (struct re_pattern_buffer * rxb, rx_Bitset cs,
  4432.            const char ** p_ptr, const char * pend,
  4433.            char * translate, reg_syntax_t syntax,
  4434.            rx_Bitset inv_tr,  char * valid_inv_tr)
  4435. #else
  4436. static reg_errcode_t
  4437. compile_range (rxb, cs, p_ptr, pend, translate, syntax, inv_tr, valid_inv_tr)
  4438.      struct re_pattern_buffer * rxb;
  4439.      rx_Bitset cs;
  4440.      const char ** p_ptr;
  4441.      const char * pend;
  4442.      char * translate;
  4443.      reg_syntax_t syntax;
  4444.      rx_Bitset inv_tr;
  4445.      char * valid_inv_tr;
  4446. #endif
  4447. {
  4448.   unsigned this_char;
  4449.  
  4450.   const char *p = *p_ptr;
  4451.  
  4452.   unsigned char range_end;
  4453.   unsigned char range_start = TRANSLATE(p[-2]);
  4454.  
  4455.   if (p == pend)
  4456.     return REG_ERANGE;
  4457.  
  4458.   PATFETCH (range_end);
  4459.  
  4460.   (*p_ptr)++;
  4461.  
  4462.   if (range_start > range_end)
  4463.     return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
  4464.  
  4465.   for (this_char = range_start; this_char <= range_end; this_char++)
  4466.     {
  4467.       rx_Bitset it =
  4468.     inverse_translation (rxb, valid_inv_tr, inv_tr, translate, this_char);
  4469.       rx_bitset_union (rxb->rx.local_cset_size, cs, it);
  4470.     }
  4471.   
  4472.   return REG_NOERROR;
  4473. }
  4474.  
  4475.  
  4476. /* This searches a regexp for backreference side effects.
  4477.  * It fills in the array OUT with 1 at the index of every register pair
  4478.  * referenced by a backreference.
  4479.  *
  4480.  * This is used to help optimize patterns for searching.  The information is
  4481.  * useful because, if the caller doesn't want register values, backreferenced
  4482.  * registers are the only registers for which we need rx_backtrack.
  4483.  */
  4484.  
  4485. #ifdef __STDC__
  4486. static void
  4487. find_backrefs (char * out, struct rexp_node * rexp,
  4488.            struct re_se_params * params)
  4489. #else
  4490. static void
  4491. find_backrefs (out, rexp, params)
  4492.      char * out;
  4493.      struct rexp_node * rexp;
  4494.      struct re_se_params * params;
  4495. #endif
  4496. {
  4497.   if (rexp)
  4498.     switch (rexp->type)
  4499.       {
  4500.       case r_cset:
  4501.       case r_data:
  4502.     return;
  4503.       case r_alternate:
  4504.       case r_concat:
  4505.       case r_opt:
  4506.       case r_star:
  4507.       case r_2phase_star:
  4508.     find_backrefs (out, rexp->params.pair.left, params);
  4509.     find_backrefs (out, rexp->params.pair.right, params);
  4510.     return;
  4511.       case r_side_effect:
  4512.     if (   ((int)rexp->params.side_effect >= 0)
  4513.         && (params [(int)rexp->params.side_effect].se == re_se_backref))
  4514.       out[ params [(int)rexp->params.side_effect].op1] = 1;
  4515.     return;
  4516.       }
  4517. }
  4518.  
  4519.  
  4520.  
  4521. /* Returns 0 unless the pattern can match the empty string. */
  4522.  
  4523. #ifdef __STDC__
  4524. static int
  4525. compute_fastset (struct re_pattern_buffer * rxb, struct rexp_node * rexp)
  4526. #else
  4527. static int
  4528. compute_fastset (rxb, rexp)
  4529.      struct re_pattern_buffer * rxb;
  4530.      struct rexp_node * rexp;
  4531. #endif
  4532. {
  4533.   if (!rexp)
  4534.     return 1;
  4535.   switch (rexp->type)
  4536.     {
  4537.     case r_data:
  4538.       return 1;
  4539.     case r_cset:
  4540.       {
  4541.     rx_bitset_union (rxb->rx.local_cset_size,
  4542.              rxb->fastset, rexp->params.cset);
  4543.       }
  4544.       return 0;
  4545.     case r_concat:
  4546.       return (compute_fastset (rxb, rexp->params.pair.left)
  4547.           && compute_fastset (rxb, rexp->params.pair.right));
  4548.     case r_2phase_star:
  4549.       compute_fastset (rxb, rexp->params.pair.left);
  4550.       /* compute_fastset (rxb, rexp->params.pair.right);  nope... */
  4551.       return 1;
  4552.     case r_alternate:
  4553.       return !!(compute_fastset (rxb, rexp->params.pair.left)
  4554.         + compute_fastset (rxb, rexp->params.pair.right));
  4555.     case r_opt:
  4556.     case r_star:
  4557.       compute_fastset (rxb, rexp->params.pair.left);
  4558.       return 1;
  4559.     case r_side_effect:
  4560.       return 1;
  4561.     }
  4562. }
  4563.  
  4564.  
  4565. /* returns
  4566.  *  1 -- yes, definately anchored by the given side effect.
  4567.  *  2 -- maybe anchored, maybe the empty string.
  4568.  *  0 -- definately not anchored
  4569.  *  There is simply no other possibility.
  4570.  */
  4571.  
  4572. #ifdef __STDC__
  4573. static int
  4574. is_anchored (struct rexp_node * rexp, rx_side_effect se)
  4575. #else
  4576. static int
  4577. is_anchored (rexp, se)
  4578.      struct rexp_node * rexp;
  4579.      rx_side_effect se;
  4580. #endif
  4581. {
  4582.   if (!rexp)
  4583.     return 2;
  4584.   switch (rexp->type)
  4585.     {
  4586.     case r_cset:
  4587.     case r_data:
  4588.       return 0;
  4589.     case r_concat:
  4590.     case r_2phase_star:
  4591.       {
  4592.     int l = is_anchored (rexp->params.pair.left, se);
  4593.     return (l == 2 ? is_anchored (rexp->params.pair.right, se) : l);
  4594.       }
  4595.     case r_alternate:
  4596.       {
  4597.     int l = is_anchored (rexp->params.pair.left, se);
  4598.     int r = l ? is_anchored (rexp->params.pair.right, se) : 0;
  4599.     return MAX (l, r);
  4600.       }
  4601.     case r_opt:
  4602.     case r_star:
  4603.       return is_anchored (rexp->params.pair.left, se) ? 2 : 0;
  4604.       
  4605.     case r_side_effect:
  4606.       return ((rexp->params.side_effect == se)
  4607.           ? 1 : 2);
  4608.     }
  4609. }
  4610.  
  4611.  
  4612. /* This removes register assignments that aren't required by backreferencing.
  4613.  * This can speed up explore_future, especially if it eliminates
  4614.  * non-determinism in the superstate NFA.
  4615.  * 
  4616.  * NEEDED is an array of characters, presumably filled in by FIND_BACKREFS.
  4617.  * The non-zero elements of the array indicate which register assignments
  4618.  * can NOT be removed from the expression.
  4619.  */
  4620.  
  4621. #ifdef __STDC__
  4622. static struct rexp_node *
  4623. remove_unecessary_side_effects (struct rx * rx, char * needed,
  4624.                 struct rexp_node * rexp,
  4625.                 struct re_se_params * params)
  4626. #else
  4627. static struct rexp_node *
  4628. remove_unecessary_side_effects (rx, needed, rexp, params)
  4629.      struct rx * rx;
  4630.      char * needed;
  4631.      struct rexp_node * rexp;
  4632.      struct re_se_params * params;
  4633. #endif
  4634. {
  4635.   struct rexp_node * l;
  4636.   struct rexp_node * r;
  4637.   if (!rexp)
  4638.     return 0;
  4639.   else
  4640.     switch (rexp->type)
  4641.       {
  4642.       case r_cset:
  4643.       case r_data:
  4644.     return rexp;
  4645.       case r_alternate:
  4646.       case r_concat:
  4647.       case r_2phase_star:
  4648.     l = remove_unecessary_side_effects (rx, needed,
  4649.                         rexp->params.pair.left, params);
  4650.     r = remove_unecessary_side_effects (rx, needed,
  4651.                         rexp->params.pair.right, params);
  4652.     if ((l && r) || (rexp->type != r_concat))
  4653.       {
  4654.         rexp->params.pair.left = l;
  4655.         rexp->params.pair.right = r;
  4656.         return rexp;
  4657.       }
  4658.     else
  4659.       {
  4660.         rexp->params.pair.left = rexp->params.pair.right = 0;
  4661.         rx_free_rexp (rx, rexp);
  4662.         return l ? l : r;
  4663.       }
  4664.       case r_opt:
  4665.       case r_star:
  4666.     l = remove_unecessary_side_effects (rx, needed,
  4667.                         rexp->params.pair.left, params);
  4668.     if (l)
  4669.       {
  4670.         rexp->params.pair.left = l;
  4671.         return rexp;
  4672.       }
  4673.     else
  4674.       {
  4675.         rexp->params.pair.left = 0;
  4676.         rx_free_rexp (rx, rexp);
  4677.         return 0;
  4678.       }
  4679.       case r_side_effect:
  4680.     {
  4681.       int se = (int)rexp->params.side_effect;
  4682.       if (   (se >= 0)
  4683.           && (params [se].op1 > 0)
  4684.           && (!needed [params [se].op1])
  4685.           && (   ((enum re_side_effects)params[se].se == re_se_lparen)
  4686.           || ((enum re_side_effects)params[se].se == re_se_rparen)))
  4687.         {
  4688.           rx_free_rexp (rx, rexp);
  4689.           return 0;
  4690.         }
  4691.       else
  4692.         return rexp;
  4693.     }
  4694.       }
  4695. }
  4696.  
  4697.  
  4698.  
  4699. #ifdef __STDC__
  4700. static int
  4701. pointless_if_repeated (struct rexp_node * node, struct re_se_params * params)
  4702. #else
  4703. static int
  4704. pointless_if_repeated (node, params)
  4705.      struct rexp_node * node;
  4706.      struct re_se_params * params;
  4707. #endif
  4708. {
  4709.   if (!node)
  4710.     return 1;
  4711.   switch (node->type)
  4712.     {
  4713.     case r_cset:
  4714.       return 0;
  4715.     case r_alternate:
  4716.     case r_concat:
  4717.     case r_2phase_star:
  4718.       return (pointless_if_repeated (node->params.pair.left, params)
  4719.           && pointless_if_repeated (node->params.pair.right, params));
  4720.     case r_opt:
  4721.     case r_star:
  4722.       return pointless_if_repeated (node->params.pair.left, params);
  4723.     case r_side_effect:
  4724.       switch (((int)node->params.side_effect < 0)
  4725.           ? (enum re_side_effects)node->params.side_effect
  4726.           : (enum re_side_effects)params[(int)node->params.side_effect].se)
  4727.     {
  4728.     case re_se_try:
  4729.     case re_se_at_dot:
  4730.     case re_se_begbuf:
  4731.     case re_se_hat:
  4732.     case re_se_wordbeg:
  4733.     case re_se_wordbound:
  4734.     case re_se_notwordbound:
  4735.     case re_se_wordend:
  4736.     case re_se_endbuf:
  4737.     case re_se_dollar:
  4738.     case re_se_fail:
  4739.     case re_se_win:
  4740.       return 1;
  4741.     case re_se_lparen:
  4742.     case re_se_rparen:
  4743.     case re_se_iter:
  4744.     case re_se_end_iter:
  4745.     case re_se_syntax:
  4746.     case re_se_not_syntax:
  4747.     case re_se_backref:
  4748.       return 0;
  4749.     }
  4750.     case r_data:
  4751.     default:
  4752.       return 0;
  4753.     }
  4754. }
  4755.  
  4756.  
  4757.  
  4758. #ifdef __STDC__
  4759. static int
  4760. registers_on_stack (struct re_pattern_buffer * rxb,
  4761.             struct rexp_node * rexp, int in_danger,
  4762.             struct re_se_params * params)
  4763. #else
  4764. static int
  4765. registers_on_stack (rxb, rexp, in_danger, params)
  4766.      struct re_pattern_buffer * rxb;
  4767.      struct rexp_node * rexp;
  4768.      int in_danger;
  4769.      struct re_se_params * params;
  4770. #endif
  4771. {
  4772.   if (!rexp)
  4773.     return 0;
  4774.   else
  4775.     switch (rexp->type)
  4776.       {
  4777.       case r_cset:
  4778.       case r_data:
  4779.     return 0;
  4780.       case r_alternate:
  4781.       case r_concat:
  4782.     return (   registers_on_stack (rxb, rexp->params.pair.left,
  4783.                        in_danger, params)
  4784.         || (registers_on_stack
  4785.             (rxb, rexp->params.pair.right,
  4786.              in_danger, params)));
  4787.       case r_opt:
  4788.     return registers_on_stack (rxb, rexp->params.pair.left, 0, params);
  4789.       case r_star:
  4790.     return registers_on_stack (rxb, rexp->params.pair.left, 1, params);
  4791.       case r_2phase_star:
  4792.     return
  4793.       (   registers_on_stack (rxb, rexp->params.pair.left, 1, params)
  4794.        || registers_on_stack (rxb, rexp->params.pair.right, 1, params));
  4795.       case r_side_effect:
  4796.     {
  4797.       int se = (int)rexp->params.side_effect;
  4798.       if (   in_danger
  4799.           && (se >= 0)
  4800.           && (params [se].op1 > 0)
  4801.           && (   ((enum re_side_effects)params[se].se == re_se_lparen)
  4802.           || ((enum re_side_effects)params[se].se == re_se_rparen)))
  4803.         return 1;
  4804.       else
  4805.         return 0;
  4806.     }
  4807.       }
  4808. }
  4809.  
  4810.  
  4811.  
  4812. static char idempotent_complex_se[] =
  4813. {
  4814. #define RX_WANT_SE_DEFS 1
  4815. #undef RX_DEF_SE
  4816. #undef RX_DEF_CPLX_SE
  4817. #define RX_DEF_SE(IDEM, NAME, VALUE)          
  4818. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE)     IDEM,
  4819. #include "rx.h"
  4820. #undef RX_DEF_SE
  4821. #undef RX_DEF_CPLX_SE
  4822. #undef RX_WANT_SE_DEFS
  4823.   23
  4824. };
  4825.  
  4826. static char idempotent_se[] =
  4827. {
  4828.   13,
  4829. #define RX_WANT_SE_DEFS 1
  4830. #undef RX_DEF_SE
  4831. #undef RX_DEF_CPLX_SE
  4832. #define RX_DEF_SE(IDEM, NAME, VALUE)          IDEM,
  4833. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE)     
  4834. #include "rx.h"
  4835. #undef RX_DEF_SE
  4836. #undef RX_DEF_CPLX_SE
  4837. #undef RX_WANT_SE_DEFS
  4838.   42
  4839. };
  4840.  
  4841. /* This must be called AFTER `convert_hard_loops' for a given REXP. */
  4842. #ifdef __STDC__
  4843. static int
  4844. has_non_idempotent_epsilon_path (struct rx * rx,
  4845.                  struct rexp_node * rexp,
  4846.                  struct re_se_params * params)
  4847. #else
  4848. static int
  4849. has_non_idempotent_epsilon_path (rx, rexp, params)
  4850.      struct rx * rx;
  4851.      struct rexp_node * rexp;
  4852.      struct re_se_params * params;
  4853. #endif
  4854. {
  4855.   if (!rexp)
  4856.     return 0;
  4857.  
  4858.   switch (rexp->type)
  4859.     {
  4860.     case r_cset:
  4861.     case r_data:
  4862.     case r_star:
  4863.       return 0;
  4864.  
  4865.     case r_side_effect:
  4866.       return
  4867.     !((int)rexp->params.side_effect > 0
  4868.       ? idempotent_complex_se [ params [(int)rexp->params.side_effect].se ]
  4869.       : idempotent_se [-(int)rexp->params.side_effect]);
  4870.       
  4871.     case r_alternate:
  4872.       return
  4873.     (   has_non_idempotent_epsilon_path (rx,
  4874.                          rexp->params.pair.left, params)
  4875.      || has_non_idempotent_epsilon_path (rx,
  4876.                          rexp->params.pair.right, params));
  4877.  
  4878.     case r_2phase_star:
  4879.     case r_concat:
  4880.       return
  4881.     (   has_non_idempotent_epsilon_path (rx,
  4882.                          rexp->params.pair.left, params)
  4883.      && has_non_idempotent_epsilon_path (rx,
  4884.                          rexp->params.pair.right, params));
  4885.  
  4886.     case r_opt:
  4887.       return has_non_idempotent_epsilon_path (rx,
  4888.                           rexp->params.pair.left, params);
  4889.     }
  4890.  
  4891. }
  4892.  
  4893.  
  4894.  
  4895.  
  4896.  
  4897. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  4898.    Returns one of error codes defined in `regex.h', or zero for success.
  4899.  
  4900.    Assumes the `allocated' (and perhaps `buffer') and `translate'
  4901.    fields are set in BUFP on entry.
  4902.  
  4903.    If it succeeds, results are put in BUFP (if it returns an error, the
  4904.    contents of BUFP are undefined):
  4905.      `buffer' is the compiled pattern;
  4906.      `syntax' is set to SYNTAX;
  4907.      `used' is set to the length of the compiled pattern;
  4908.      `fastmap_accurate' is set to zero;
  4909.      `re_nsub' is set to the number of groups in PATTERN;
  4910.      `not_bol' and `not_eol' are set to zero.
  4911.    
  4912.    The `fastmap' and `newline_anchor' fields are neither
  4913.    examined nor set.  */
  4914.  
  4915.  
  4916.  
  4917. #ifdef __STDC__
  4918. reg_errcode_t
  4919. rx_compile (const char *pattern, int size,
  4920.         reg_syntax_t syntax,
  4921.         struct re_pattern_buffer * rxb) 
  4922. #else
  4923. reg_errcode_t
  4924. rx_compile (pattern, size, syntax, rxb)
  4925.      const char *pattern;
  4926.      int size;
  4927.      reg_syntax_t syntax;
  4928.      struct re_pattern_buffer * rxb;
  4929. #endif
  4930. {
  4931.   RX_subset
  4932.     inverse_translate [CHAR_SET_SIZE * rx_bitset_numb_subsets(CHAR_SET_SIZE)];
  4933.   char
  4934.     validate_inv_tr [CHAR_SET_SIZE * rx_bitset_numb_subsets(CHAR_SET_SIZE)];
  4935.  
  4936.   /* We fetch characters from PATTERN here.  Even though PATTERN is
  4937.      `char *' (i.e., signed), we declare these variables as unsigned, so
  4938.      they can be reliably used as array indices.  */
  4939.   register unsigned char c, c1;
  4940.   
  4941.   /* A random tempory spot in PATTERN.  */
  4942.   const char *p1;
  4943.   
  4944.   /* Keeps track of unclosed groups.  */
  4945.   compile_stack_type compile_stack;
  4946.  
  4947.   /* Points to the current (ending) position in the pattern.  */
  4948.   const char *p = pattern;
  4949.   const char *pend = pattern + size;
  4950.   
  4951.   /* How to translate the characters in the pattern.  */
  4952.   char *translate = rxb->translate ? rxb->translate : id_translation;
  4953.  
  4954.   /* When parsing is done, this will hold the expression tree. */
  4955.   struct rexp_node * rexp = 0;
  4956.  
  4957.   /* In the midst of compilation, this holds onto the regexp 
  4958.    * first parst while rexp goes on to aquire additional constructs.
  4959.    */
  4960.   struct rexp_node * orig_rexp = 0;
  4961.   struct rexp_node * fewer_side_effects = 0;
  4962.  
  4963.   /* This and top_expression are saved on the compile stack. */
  4964.   struct rexp_node ** top_expression = &rexp;
  4965.   struct rexp_node ** last_expression = top_expression;
  4966.   
  4967.   /* Parameter to `goto append_node' */
  4968.   struct rexp_node * append;
  4969.  
  4970.   /* Counts open-groups as they are encountered.  This is the index of the
  4971.    * innermost group being compiled.
  4972.    */
  4973.   regnum_t regnum = 0;
  4974.  
  4975.   /* Place in the uncompiled pattern (i.e., the {) to
  4976.    * which to go back if the interval is invalid.  
  4977.    */
  4978.   const char *beg_interval;
  4979.  
  4980.   struct re_se_params * params = 0;
  4981.   int paramc = 0;        /* How many complex side effects so far? */
  4982.  
  4983.   rx_side_effect side;        /* param to `goto add_side_effect' */
  4984.  
  4985.   bzero (validate_inv_tr, sizeof (validate_inv_tr));
  4986.  
  4987.   rxb->rx.instruction_table = rx_id_instruction_table;
  4988.  
  4989.  
  4990.   /* Initialize the compile stack.  */
  4991.   compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  4992.   if (compile_stack.stack == 0)
  4993.     return REG_ESPACE;
  4994.  
  4995.   compile_stack.size = INIT_COMPILE_STACK_SIZE;
  4996.   compile_stack.avail = 0;
  4997.  
  4998.   /* Initialize the pattern buffer.  */
  4999.   rxb->rx.cache = &default_cache;
  5000.   rxb->syntax = syntax;
  5001.   rxb->fastmap_accurate = 0;
  5002.   rxb->not_bol = rxb->not_eol = 0;
  5003.   rxb->least_subs = 0;
  5004.   
  5005.   /* Always count groups, whether or not rxb->no_sub is set.  
  5006.    * The whole pattern is implicitly group 0, so counting begins
  5007.    * with 1.
  5008.    */
  5009.   rxb->re_nsub = 0;
  5010.  
  5011. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  5012.   /* Initialize the syntax table.  */
  5013.    init_syntax_once ();
  5014. #endif
  5015.  
  5016.   /* Loop through the uncompiled pattern until we're at the end.  */
  5017.   while (p != pend)
  5018.     {
  5019.       PATFETCH (c);
  5020.  
  5021.       switch (c)
  5022.         {
  5023.         case '^':
  5024.           {
  5025.             if (   /* If at start of pattern, it's an operator.  */
  5026.                    p == pattern + 1
  5027.                    /* If context independent, it's an operator.  */
  5028.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  5029.                    /* Otherwise, depends on what's come before.  */
  5030.                 || at_begline_loc_p (pattern, p, syntax))
  5031.           {
  5032.         struct rexp_node * n
  5033.           = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)re_se_hat);
  5034.         if (!n)
  5035.           return REG_ESPACE;
  5036.         append = n;
  5037.         goto append_node;
  5038.           }
  5039.             else
  5040.               goto normal_char;
  5041.           }
  5042.           break;
  5043.  
  5044.  
  5045.         case '$':
  5046.           {
  5047.             if (   /* If at end of pattern, it's an operator.  */
  5048.                    p == pend 
  5049.                    /* If context independent, it's an operator.  */
  5050.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  5051.                    /* Otherwise, depends on what's next.  */
  5052.                 || at_endline_loc_p (p, pend, syntax))
  5053.           {
  5054.         struct rexp_node * n
  5055.           = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)re_se_dollar);
  5056.         if (!n)
  5057.           return REG_ESPACE;
  5058.         append = n;
  5059.         goto append_node;
  5060.           }
  5061.              else
  5062.                goto normal_char;
  5063.            }
  5064.            break;
  5065.  
  5066.  
  5067.     case '+':
  5068.         case '?':
  5069.           if ((syntax & RE_BK_PLUS_QM)
  5070.               || (syntax & RE_LIMITED_OPS))
  5071.             goto normal_char;
  5072.  
  5073.         handle_plus:
  5074.         case '*':
  5075.           /* If there is no previous pattern... */
  5076.           if (pointless_if_repeated (*last_expression, params))
  5077.             {
  5078.               if (syntax & RE_CONTEXT_INVALID_OPS)
  5079.                 return REG_BADRPT;
  5080.               else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  5081.                 goto normal_char;
  5082.             }
  5083.  
  5084.           {
  5085.             /* 1 means zero (many) matches is allowed.  */
  5086.             char zero_times_ok = 0, many_times_ok = 0;
  5087.  
  5088.             /* If there is a sequence of repetition chars, collapse it
  5089.                down to just one (the right one).  We can't combine
  5090.                interval operators with these because of, e.g., `a{2}*',
  5091.                which should only match an even number of `a's.  */
  5092.  
  5093.             for (;;)
  5094.               {
  5095.                 zero_times_ok |= c != '+';
  5096.                 many_times_ok |= c != '?';
  5097.  
  5098.                 if (p == pend)
  5099.                   break;
  5100.  
  5101.                 PATFETCH (c);
  5102.  
  5103.                 if (c == '*'
  5104.                     || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  5105.                   ;
  5106.  
  5107.                 else if (syntax & RE_BK_PLUS_QM  &&  c == '\\')
  5108.                   {
  5109.                     if (p == pend) return REG_EESCAPE;
  5110.  
  5111.                     PATFETCH (c1);
  5112.                     if (!(c1 == '+' || c1 == '?'))
  5113.                       {
  5114.                         PATUNFETCH;
  5115.                         PATUNFETCH;
  5116.                         break;
  5117.                       }
  5118.  
  5119.                     c = c1;
  5120.                   }
  5121.                 else
  5122.                   {
  5123.                     PATUNFETCH;
  5124.                     break;
  5125.                   }
  5126.  
  5127.                 /* If we get here, we found another repeat character.  */
  5128.                }
  5129.  
  5130.             /* Star, etc. applied to an empty pattern is equivalent
  5131.                to an empty pattern.  */
  5132.             if (!last_expression)
  5133.               break;
  5134.  
  5135.         /* Now we know whether or not zero matches is allowed
  5136.          * and also whether or not two or more matches is allowed.
  5137.          */
  5138.  
  5139.         {
  5140.           struct rexp_node * inner_exp = *last_expression;
  5141.  
  5142.           if (many_times_ok
  5143.           && has_non_idempotent_epsilon_path (&rxb->rx,
  5144.                               inner_exp, params))
  5145.         {
  5146.           struct rexp_node * pusher
  5147.             = rx_mk_r_side_effect (&rxb->rx,
  5148.                        (rx_side_effect)re_se_pushpos);
  5149.           struct rexp_node * checker
  5150.             = rx_mk_r_side_effect (&rxb->rx,
  5151.                        (rx_side_effect)re_se_chkpos);
  5152.           struct rexp_node * pushback
  5153.             = rx_mk_r_side_effect (&rxb->rx,
  5154.                        (rx_side_effect)re_se_pushback);
  5155.           rx_Bitset cs = rx_cset (&rxb->rx);
  5156.           struct rexp_node * lit_t = rx_mk_r_cset (&rxb->rx, cs);
  5157.           struct rexp_node * fake_state
  5158.             = rx_mk_r_concat (&rxb->rx, pushback, lit_t);
  5159.           struct rexp_node * phase2
  5160.             = rx_mk_r_concat (&rxb->rx, checker, fake_state);
  5161.           struct rexp_node * popper
  5162.             = rx_mk_r_side_effect (&rxb->rx,
  5163.                        (rx_side_effect)re_se_poppos);
  5164.           struct rexp_node * star
  5165.             = rx_mk_r_2phase_star (&rxb->rx, inner_exp, phase2);
  5166.           struct rexp_node * a
  5167.             = rx_mk_r_concat (&rxb->rx, pusher, star);
  5168.           struct rexp_node * whole_thing
  5169.             = rx_mk_r_concat (&rxb->rx, a, popper);
  5170.           if (!(pusher && star && pushback && lit_t && fake_state
  5171.             && lit_t && phase2 && checker && popper
  5172.             && a && whole_thing))
  5173.             return REG_ESPACE;
  5174.           RX_bitset_enjoin (cs, 't');
  5175.           *last_expression = whole_thing;
  5176.         }
  5177.           else
  5178.         {
  5179.           struct rexp_node * star =
  5180.             (many_times_ok ? rx_mk_r_star : rx_mk_r_opt)
  5181.               (&rxb->rx, *last_expression);
  5182.           if (!star)
  5183.             return REG_ESPACE;
  5184.           *last_expression = star;
  5185.         }
  5186.           if (!zero_times_ok)
  5187.         {
  5188.           struct rexp_node * concat
  5189.             = rx_mk_r_concat (&rxb->rx, inner_exp,
  5190.                       rx_copy_rexp (&rxb->rx, *last_expression));
  5191.           if (!concat)
  5192.             return REG_ESPACE;
  5193.           *last_expression = concat;
  5194.         }
  5195.         }
  5196.         /* The old regex.c used to optimize `.*\n'.  
  5197.          * Maybe rx should too. 
  5198.          */
  5199.       }
  5200.       break;
  5201.  
  5202.  
  5203.     case '.':
  5204.       {
  5205.         rx_Bitset cs = rx_cset (&rxb->rx);
  5206.         struct rexp_node * n = rx_mk_r_cset (&rxb->rx, cs);
  5207.         if (!(cs && n))
  5208.           return REG_ESPACE;
  5209.  
  5210.         rx_bitset_universe (rxb->rx.local_cset_size, cs);
  5211.         if (!(rxb->syntax & RE_DOT_NEWLINE))
  5212.           RX_bitset_remove (cs, '\n');
  5213.         if (!(rxb->syntax & RE_DOT_NOT_NULL))
  5214.           RX_bitset_remove (cs, 0);
  5215.  
  5216.         append = n;
  5217.         goto append_node;
  5218.         break;
  5219.       }
  5220.  
  5221.  
  5222.         case '[':
  5223.       if (p == pend) return REG_EBRACK;
  5224.           {
  5225.             boolean had_char_class = false;
  5226.         rx_Bitset cs = rx_cset (&rxb->rx);
  5227.         struct rexp_node * node = rx_mk_r_cset (&rxb->rx, cs);
  5228.         int is_inverted = *p == '^';
  5229.         
  5230.         if (!(node && cs))
  5231.           return REG_ESPACE;
  5232.         
  5233.         /* This branch of the switch is normally exited with
  5234.          *`goto append_node'
  5235.          */
  5236.         append = node;
  5237.         
  5238.             if (is_inverted)
  5239.           p++;
  5240.         
  5241.             /* Remember the first position in the bracket expression.  */
  5242.             p1 = p;
  5243.         
  5244.             /* Read in characters and ranges, setting map bits.  */
  5245.             for (;;)
  5246.               {
  5247.                 if (p == pend) return REG_EBRACK;
  5248.         
  5249.                 PATFETCH (c);
  5250.         
  5251.                 /* \ might escape characters inside [...] and [^...].  */
  5252.                 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  5253.                   {
  5254.                     if (p == pend) return REG_EESCAPE;
  5255.             
  5256.                     PATFETCH (c1);
  5257.             {
  5258.               rx_Bitset it = inverse_translation (rxb, 
  5259.                               validate_inv_tr,
  5260.                               inverse_translate,
  5261.                               translate,
  5262.                               c1);
  5263.               rx_bitset_union (rxb->rx.local_cset_size, cs, it);
  5264.             }
  5265.                     continue;
  5266.                   }
  5267.         
  5268.                 /* Could be the end of the bracket expression.  If it's
  5269.                    not (i.e., when the bracket expression is `[]' so
  5270.                    far), the ']' character bit gets set way below.  */
  5271.                 if (c == ']' && p != p1 + 1)
  5272.                   goto finalize_class_and_append;
  5273.         
  5274.                 /* Look ahead to see if it's a range when the last thing
  5275.                    was a character class.  */
  5276.                 if (had_char_class && c == '-' && *p != ']')
  5277.                   return REG_ERANGE;
  5278.         
  5279.                 /* Look ahead to see if it's a range when the last thing
  5280.                    was a character: if this is a hyphen not at the
  5281.                    beginning or the end of a list, then it's the range
  5282.                    operator.  */
  5283.                 if (c == '-' 
  5284.                     && !(p - 2 >= pattern && p[-2] == '[') 
  5285.                     && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  5286.                     && *p != ']')
  5287.                   {
  5288.                     reg_errcode_t ret
  5289.                       = compile_range (rxb, cs, &p, pend, translate, syntax,
  5290.                        inverse_translate, validate_inv_tr);
  5291.                     if (ret != REG_NOERROR) return ret;
  5292.                   }
  5293.         
  5294.                 else if (p[0] == '-' && p[1] != ']')
  5295.                   { /* This handles ranges made up of characters only.  */
  5296.                     reg_errcode_t ret;
  5297.             
  5298.             /* Move past the `-'.  */
  5299.                     PATFETCH (c1);
  5300.                     
  5301.                     ret = compile_range (rxb, cs, &p, pend, translate, syntax,
  5302.                      inverse_translate, validate_inv_tr);
  5303.                     if (ret != REG_NOERROR) return ret;
  5304.                   }
  5305.         
  5306.                 /* See if we're at the beginning of a possible character
  5307.                    class.  */
  5308.         
  5309.         else if ((syntax & RE_CHAR_CLASSES)
  5310.              && (c == '[') && (*p == ':'))
  5311.                   {
  5312.                     char str[CHAR_CLASS_MAX_LENGTH + 1];
  5313.             
  5314.                     PATFETCH (c);
  5315.                     c1 = 0;
  5316.             
  5317.                     /* If pattern is `[[:'.  */
  5318.                     if (p == pend) return REG_EBRACK;
  5319.             
  5320.                     for (;;)
  5321.                       {
  5322.                         PATFETCH (c);
  5323.                         if (c == ':' || c == ']' || p == pend
  5324.                             || c1 == CHAR_CLASS_MAX_LENGTH)
  5325.               break;
  5326.                         str[c1++] = c;
  5327.                       }
  5328.                     str[c1] = '\0';
  5329.             
  5330.                     /* If isn't a word bracketed by `[:' and:`]':
  5331.                        undo the ending character, the letters, and leave 
  5332.                        the leading `:' and `[' (but set bits for them).  */
  5333.                     if (c == ':' && *p == ']')
  5334.                       {
  5335.                         int ch;
  5336.                         boolean is_alnum = !strcmp (str, "alnum");
  5337.                         boolean is_alpha = !strcmp (str, "alpha");
  5338.                         boolean is_blank = !strcmp (str, "blank");
  5339.                         boolean is_cntrl = !strcmp (str, "cntrl");
  5340.                         boolean is_digit = !strcmp (str, "digit");
  5341.                         boolean is_graph = !strcmp (str, "graph");
  5342.                         boolean is_lower = !strcmp (str, "lower");
  5343.                         boolean is_print = !strcmp (str, "print");
  5344.                         boolean is_punct = !strcmp (str, "punct");
  5345.                         boolean is_space = !strcmp (str, "space");
  5346.                         boolean is_upper = !strcmp (str, "upper");
  5347.                         boolean is_xdigit = !strcmp (str, "xdigit");
  5348.                         
  5349.                         if (!IS_CHAR_CLASS (str)) return REG_ECTYPE;
  5350.             
  5351.                         /* Throw away the ] at the end of the character
  5352.                            class.  */
  5353.                         PATFETCH (c);                    
  5354.             
  5355.                         if (p == pend) return REG_EBRACK;
  5356.             
  5357.                         for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  5358.                           {
  5359.                             if (   (is_alnum  && isalnum (ch))
  5360.                                 || (is_alpha  && isalpha (ch))
  5361.                                 || (is_blank  && isblank (ch))
  5362.                                 || (is_cntrl  && iscntrl (ch))
  5363.                                 || (is_digit  && isdigit (ch))
  5364.                                 || (is_graph  && isgraph (ch))
  5365.                                 || (is_lower  && islower (ch))
  5366.                                 || (is_print  && isprint (ch))
  5367.                                 || (is_punct  && ispunct (ch))
  5368.                                 || (is_space  && isspace (ch))
  5369.                                 || (is_upper  && isupper (ch))
  5370.                                 || (is_xdigit && isxdigit (ch)))
  5371.                   {
  5372.                 rx_Bitset it =
  5373.                   inverse_translation (rxb, 
  5374.                                validate_inv_tr,
  5375.                                inverse_translate,
  5376.                                translate,
  5377.                                ch);
  5378.                 rx_bitset_union (rxb->rx.local_cset_size,
  5379.                          cs, it);
  5380.                   }
  5381.                           }
  5382.                         had_char_class = true;
  5383.                       }
  5384.                     else
  5385.                       {
  5386.                         c1++;
  5387.                         while (c1--)    
  5388.                           PATUNFETCH;
  5389.             {
  5390.               rx_Bitset it =
  5391.                 inverse_translation (rxb, 
  5392.                          validate_inv_tr,
  5393.                          inverse_translate,
  5394.                          translate,
  5395.                          '[');
  5396.               rx_bitset_union (rxb->rx.local_cset_size,
  5397.                        cs, it);
  5398.             }
  5399.             {
  5400.               rx_Bitset it =
  5401.                 inverse_translation (rxb, 
  5402.                          validate_inv_tr,
  5403.                          inverse_translate,
  5404.                          translate,
  5405.                          ':');
  5406.               rx_bitset_union (rxb->rx.local_cset_size,
  5407.                        cs, it);
  5408.             }
  5409.                         had_char_class = false;
  5410.                       }
  5411.                   }
  5412.                 else
  5413.                   {
  5414.                     had_char_class = false;
  5415.             {
  5416.               rx_Bitset it = inverse_translation (rxb, 
  5417.                               validate_inv_tr,
  5418.                               inverse_translate,
  5419.                               translate,
  5420.                               c);
  5421.               rx_bitset_union (rxb->rx.local_cset_size, cs, it);
  5422.             }
  5423.                   }
  5424.               }
  5425.  
  5426.       finalize_class_and_append:
  5427.         if (is_inverted)
  5428.           {
  5429.         rx_bitset_complement (rxb->rx.local_cset_size, cs);
  5430.         if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
  5431.           RX_bitset_remove (cs, '\n');
  5432.           }
  5433.         goto append_node;
  5434.           }
  5435.           break;
  5436.  
  5437.  
  5438.     case '(':
  5439.           if (syntax & RE_NO_BK_PARENS)
  5440.             goto handle_open;
  5441.           else
  5442.             goto normal_char;
  5443.  
  5444.  
  5445.         case ')':
  5446.           if (syntax & RE_NO_BK_PARENS)
  5447.             goto handle_close;
  5448.           else
  5449.             goto normal_char;
  5450.  
  5451.  
  5452.         case '\n':
  5453.           if (syntax & RE_NEWLINE_ALT)
  5454.             goto handle_alt;
  5455.           else
  5456.             goto normal_char;
  5457.  
  5458.  
  5459.     case '|':
  5460.           if (syntax & RE_NO_BK_VBAR)
  5461.             goto handle_alt;
  5462.           else
  5463.             goto normal_char;
  5464.  
  5465.  
  5466.         case '{':
  5467.       if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  5468.         goto handle_interval;
  5469.       else
  5470.         goto normal_char;
  5471.  
  5472.  
  5473.         case '\\':
  5474.           if (p == pend) return REG_EESCAPE;
  5475.  
  5476.           /* Do not translate the character after the \, so that we can
  5477.              distinguish, e.g., \B from \b, even if we normally would
  5478.              translate, e.g., B to b.  */
  5479.           PATFETCH_RAW (c);
  5480.  
  5481.           switch (c)
  5482.             {
  5483.             case '(':
  5484.               if (syntax & RE_NO_BK_PARENS)
  5485.                 goto normal_backslash;
  5486.  
  5487.             handle_open:
  5488.               rxb->re_nsub++;
  5489.               regnum++;
  5490.               if (COMPILE_STACK_FULL)
  5491.                 { 
  5492.                   RETALLOC (compile_stack.stack, compile_stack.size << 1,
  5493.                             compile_stack_elt_t);
  5494.                   if (compile_stack.stack == 0) return REG_ESPACE;
  5495.  
  5496.                   compile_stack.size <<= 1;
  5497.                 }
  5498.  
  5499.           if (*last_expression)
  5500.         {
  5501.           struct rexp_node * concat
  5502.             = rx_mk_r_concat (&rxb->rx, *last_expression, 0);
  5503.           if (!concat)
  5504.             return REG_ESPACE;
  5505.           *last_expression = concat;
  5506.           last_expression = &concat->params.pair.right;
  5507.         }
  5508.  
  5509.               /*
  5510.            * These are the values to restore when we hit end of this
  5511.                * group.  
  5512.            */
  5513.           COMPILE_STACK_TOP.top_expression = top_expression;
  5514.           COMPILE_STACK_TOP.last_expression = last_expression;
  5515.               COMPILE_STACK_TOP.regnum = regnum;
  5516.           
  5517.               compile_stack.avail++;
  5518.           
  5519.           top_expression = last_expression;
  5520.           break;
  5521.  
  5522.  
  5523.             case ')':
  5524.               if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  5525.  
  5526.             handle_close:
  5527.               /* See similar code for backslashed left paren above.  */
  5528.               if (COMPILE_STACK_EMPTY)
  5529.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  5530.                   goto normal_char;
  5531.                 else
  5532.                   return REG_ERPAREN;
  5533.  
  5534.               /* Since we just checked for an empty stack above, this
  5535.                  ``can't happen''.  */
  5536.  
  5537.               {
  5538.                 /* We don't just want to restore into `regnum', because
  5539.                    later groups should continue to be numbered higher,
  5540.                    as in `(ab)c(de)' -- the second group is #2.  */
  5541.                 regnum_t this_group_regnum;
  5542.         struct rexp_node ** inner = top_expression;
  5543.  
  5544.                 compile_stack.avail--;
  5545.         top_expression = COMPILE_STACK_TOP.top_expression;
  5546.         last_expression = COMPILE_STACK_TOP.last_expression;
  5547.                 this_group_regnum = COMPILE_STACK_TOP.regnum;
  5548.         {
  5549.           int left_se = paramc;
  5550.           int right_se = paramc + 1;
  5551.  
  5552.           params = (params
  5553.                 ? ((struct re_se_params *)
  5554.                    realloc (params,
  5555.                     (paramc + 2) * sizeof (params[0])))
  5556.                 : ((struct re_se_params *)
  5557.                    malloc (2 * sizeof (params[0]))));
  5558.           if (!params)
  5559.             return REG_ESPACE;
  5560.           paramc += 2;
  5561.  
  5562.           params[left_se].se = re_se_lparen;
  5563.           params[left_se].op1 = this_group_regnum;
  5564.           params[right_se].se = re_se_rparen;
  5565.           params[right_se].op1 = this_group_regnum;
  5566.           {
  5567.             struct rexp_node * left
  5568.               = rx_mk_r_side_effect (&rxb->rx,
  5569.                          (rx_side_effect)left_se);
  5570.             struct rexp_node * right
  5571.               = rx_mk_r_side_effect (&rxb->rx,
  5572.                          (rx_side_effect)right_se);
  5573.             struct rexp_node * c1
  5574.               = (*inner
  5575.              ? rx_mk_r_concat (&rxb->rx, left, *inner) : left);
  5576.             struct rexp_node * c2
  5577.               = rx_mk_r_concat (&rxb->rx, c1, right);
  5578.             if (!(left && right && c1 && c2))
  5579.               return REG_ESPACE;
  5580.             *inner = c2;
  5581.           }
  5582.         }
  5583.         break;
  5584.           }
  5585.  
  5586.             case '|':                    /* `\|'.  */
  5587.               if ((syntax & RE_LIMITED_OPS) || (syntax & RE_NO_BK_VBAR))
  5588.                 goto normal_backslash;
  5589.             handle_alt:
  5590.               if (syntax & RE_LIMITED_OPS)
  5591.                 goto normal_char;
  5592.  
  5593.           {
  5594.         struct rexp_node * alt
  5595.           = rx_mk_r_alternate (&rxb->rx, *top_expression, 0);
  5596.         if (!alt)
  5597.           return REG_ESPACE;
  5598.         *top_expression = alt;
  5599.         last_expression = &alt->params.pair.right;
  5600.           }
  5601.               break;
  5602.  
  5603.  
  5604.             case '{': 
  5605.               /* If \{ is a literal.  */
  5606.               if (!(syntax & RE_INTERVALS)
  5607.                      /* If we're at `\{' and it's not the open-interval 
  5608.                         operator.  */
  5609.                   || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  5610.                   || (p - 2 == pattern  &&  p == pend))
  5611.                 goto normal_backslash;
  5612.  
  5613.             handle_interval:
  5614.               {
  5615.                 /* If got here, then the syntax allows intervals.  */
  5616.  
  5617.                 /* At least (most) this many matches must be made.  */
  5618.                 int lower_bound = -1, upper_bound = -1;
  5619.  
  5620.                 beg_interval = p - 1;
  5621.  
  5622.                 if (p == pend)
  5623.                   {
  5624.                     if (syntax & RE_NO_BK_BRACES)
  5625.                       goto unfetch_interval;
  5626.                     else
  5627.                       return REG_EBRACE;
  5628.                   }
  5629.  
  5630.                 GET_UNSIGNED_NUMBER (lower_bound);
  5631.  
  5632.                 if (c == ',')
  5633.                   {
  5634.                     GET_UNSIGNED_NUMBER (upper_bound);
  5635.                     if (upper_bound < 0) upper_bound = RE_DUP_MAX;
  5636.                   }
  5637.                 else
  5638.                   /* Interval such as `{1}' => match exactly once. */
  5639.                   upper_bound = lower_bound;
  5640.  
  5641.                 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  5642.                     || lower_bound > upper_bound)
  5643.                   {
  5644.                     if (syntax & RE_NO_BK_BRACES)
  5645.                       goto unfetch_interval;
  5646.                     else 
  5647.                       return REG_BADBR;
  5648.                   }
  5649.  
  5650.                 if (!(syntax & RE_NO_BK_BRACES)) 
  5651.                   {
  5652.                     if (c != '\\') return REG_EBRACE;
  5653.                     PATFETCH (c);
  5654.                   }
  5655.  
  5656.                 if (c != '}')
  5657.                   {
  5658.                     if (syntax & RE_NO_BK_BRACES)
  5659.                       goto unfetch_interval;
  5660.                     else 
  5661.                       return REG_BADBR;
  5662.                   }
  5663.  
  5664.                 /* We just parsed a valid interval.  */
  5665.  
  5666.                 /* If it's invalid to have no preceding re.  */
  5667.                 if (pointless_if_repeated (*last_expression, params))
  5668.                   {
  5669.                     if (syntax & RE_CONTEXT_INVALID_OPS)
  5670.                       return REG_BADRPT;
  5671.                     else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  5672.                       goto unfetch_interval;
  5673.             /* was: else laststart = b; */
  5674.                   }
  5675.  
  5676.                 /* If the upper bound is zero, don't want to iterate
  5677.                  * at all.
  5678.          */
  5679.                  if (upper_bound == 0)
  5680.            {
  5681.              if (*last_expression)
  5682.                {
  5683.              rx_free_rexp (&rxb->rx, *last_expression);
  5684.              *last_expression = 0;
  5685.                }
  5686.            }
  5687.         else
  5688.           /* Otherwise, we have a nontrivial interval. */
  5689.           {
  5690.             int iter_se = paramc;
  5691.             int end_se = paramc + 1;
  5692.             params = (params
  5693.                   ? ((struct re_se_params *)
  5694.                  realloc (params,
  5695.                       sizeof (*params) * (2 + paramc)))
  5696.                   : ((struct re_se_params *)
  5697.                  malloc (2 * sizeof (*params))));
  5698.             if (!params)
  5699.               return REG_ESPACE;
  5700.             paramc += 2;
  5701.             params [iter_se].se = re_se_iter;
  5702.             params [iter_se].op1 = lower_bound;
  5703.             params[iter_se].op2 = upper_bound;
  5704.  
  5705.             params[end_se].se = re_se_end_iter;
  5706.             params[end_se].op1 = lower_bound;
  5707.             params[end_se].op2 = upper_bound;
  5708.             {
  5709.               struct rexp_node * push0
  5710.             = rx_mk_r_side_effect (&rxb->rx,
  5711.                            (rx_side_effect)re_se_push0);
  5712.               struct rexp_node * start_one_iter
  5713.             = rx_mk_r_side_effect (&rxb->rx,
  5714.                            (rx_side_effect)iter_se);
  5715.               struct rexp_node * phase1
  5716.             = rx_mk_r_concat (&rxb->rx, start_one_iter,
  5717.                       *last_expression);
  5718.               struct rexp_node * pushback
  5719.             = rx_mk_r_side_effect (&rxb->rx,
  5720.                            (rx_side_effect)re_se_pushback);
  5721.               rx_Bitset cs = rx_cset (&rxb->rx);
  5722.               struct rexp_node * lit_t
  5723.             = rx_mk_r_cset (&rxb->rx, cs);
  5724.               struct rexp_node * phase2
  5725.             = rx_mk_r_concat (&rxb->rx, pushback, lit_t);
  5726.               struct rexp_node * loop
  5727.             = rx_mk_r_2phase_star (&rxb->rx, phase1, phase2);
  5728.               struct rexp_node * push_n_loop
  5729.             = rx_mk_r_concat (&rxb->rx, push0, loop);
  5730.               struct rexp_node * final_test
  5731.             = rx_mk_r_side_effect (&rxb->rx,
  5732.                            (rx_side_effect)end_se);
  5733.               struct rexp_node * full_exp
  5734.             = rx_mk_r_concat (&rxb->rx, push_n_loop, final_test);
  5735.  
  5736.               if (!(push0 && start_one_iter && phase1
  5737.                 && pushback && lit_t && phase2
  5738.                 && loop && push_n_loop && final_test && full_exp))
  5739.             return REG_ESPACE;
  5740.  
  5741.               RX_bitset_enjoin(cs, 't');
  5742.  
  5743.               *last_expression = full_exp;
  5744.             }
  5745.           }
  5746.                 beg_interval = 0;
  5747.               }
  5748.               break;
  5749.  
  5750.             unfetch_interval:
  5751.               /* If an invalid interval, match the characters as literals.  */
  5752.                p = beg_interval;
  5753.                beg_interval = NULL;
  5754.  
  5755.                /* normal_char and normal_backslash need `c'.  */
  5756.                PATFETCH (c);    
  5757.  
  5758.                if (!(syntax & RE_NO_BK_BRACES))
  5759.                  {
  5760.                    if (p > pattern  &&  p[-1] == '\\')
  5761.                      goto normal_backslash;
  5762.                  }
  5763.                goto normal_char;
  5764.  
  5765. #ifdef emacs
  5766.             /* There is no way to specify the before_dot and after_dot
  5767.                operators.  rms says this is ok.  --karl  */
  5768.             case '=':
  5769.           side = at_dot;
  5770.           goto add_side_effect;
  5771.               break;
  5772.  
  5773.             case 's':
  5774.         case 'S':
  5775.           {
  5776.         rx_Bitset cs = cset (&rxb->rx);
  5777.         struct rexp_node * set = rx_mk_r_cset (&rxb->rx, cs);
  5778.         if (!(cs && set))
  5779.           return REG_ESPACE;
  5780.         if (c == 'S')
  5781.           rx_bitset_universe (rxb->rx.local_cset_size, cs);
  5782.  
  5783.         PATFETCH (c);
  5784.         {
  5785.           int x;
  5786.           char code = syntax_spec_code (c);
  5787.           for (x = 0; x < 256; ++x)
  5788.             {
  5789.               
  5790.               if (SYNTAX (x) & code)
  5791.             {
  5792.               rx_Bitset it =
  5793.                 inverse_translation (rxb, validate_inv_tr,
  5794.                          inverse_translate,
  5795.                          translate, x);
  5796.               rx_bitset_xor (rxb->rx.local_cset_size, cs, it);
  5797.             }
  5798.             }
  5799.         }
  5800.         goto append_node;
  5801.           }
  5802.               break;
  5803. #endif /* emacs */
  5804.  
  5805.  
  5806.             case 'w':
  5807.             case 'W':
  5808.           {
  5809.         rx_Bitset cs = rx_cset (&rxb->rx);
  5810.         struct rexp_node * n = (cs ? rx_mk_r_cset (&rxb->rx, cs) : 0);
  5811.         if (!(cs && n))
  5812.           return REG_ESPACE;
  5813.         if (c == 'W')
  5814.           rx_bitset_universe (rxb->rx.local_cset_size ,cs);
  5815.         {
  5816.           int x;
  5817.           for (x = rxb->rx.local_cset_size - 1; x > 0; --x)
  5818.             if (re_syntax_table[x] & Sword)
  5819.               RX_bitset_toggle (cs, x);
  5820.         }
  5821.         append = n;
  5822.         goto append_node;
  5823.           }
  5824.               break;
  5825.  
  5826. /* With a little extra work, some of these side effects could be optimized
  5827.  * away (basicly by looking at what we already know about the surrounding
  5828.  * chars).  
  5829.  */
  5830.             case '<':
  5831.           side = (rx_side_effect)re_se_wordbeg;
  5832.           goto add_side_effect;
  5833.               break;
  5834.  
  5835.             case '>':
  5836.               side = (rx_side_effect)re_se_wordend;
  5837.           goto add_side_effect;
  5838.               break;
  5839.  
  5840.             case 'b':
  5841.               side = (rx_side_effect)re_se_wordbound;
  5842.           goto add_side_effect;
  5843.               break;
  5844.  
  5845.             case 'B':
  5846.               side = (rx_side_effect)re_se_notwordbound;
  5847.           goto add_side_effect;
  5848.               break;
  5849.  
  5850.             case '`':
  5851.           side = (rx_side_effect)re_se_begbuf;
  5852.           goto add_side_effect;
  5853.           break;
  5854.           
  5855.             case '\'':
  5856.           side = (rx_side_effect)re_se_endbuf;
  5857.           goto add_side_effect;
  5858.               break;
  5859.  
  5860.         add_side_effect:
  5861.           {
  5862.         struct rexp_node * se
  5863.           = rx_mk_r_side_effect (&rxb->rx, side);
  5864.         if (!se)
  5865.           return REG_ESPACE;
  5866.         append = se;
  5867.         goto append_node;
  5868.           }
  5869.           break;
  5870.  
  5871.             case '1': case '2': case '3': case '4': case '5':
  5872.             case '6': case '7': case '8': case '9':
  5873.               if (syntax & RE_NO_BK_REFS)
  5874.                 goto normal_char;
  5875.  
  5876.               c1 = c - '0';
  5877.  
  5878.               if (c1 > regnum)
  5879.                 return REG_ESUBREG;
  5880.  
  5881.               /* Can't back reference to a subexpression if inside of it.  */
  5882.               if (group_in_compile_stack (compile_stack, c1))
  5883.                 goto normal_char;
  5884.           {
  5885.         int backref_se = paramc;
  5886.         params = (params
  5887.               ? ((struct re_se_params *)
  5888.                  realloc (params,
  5889.                       sizeof (*params) * (1 + paramc)))
  5890.               : ((struct re_se_params *)
  5891.                  malloc (sizeof (*params))));
  5892.         if (!params)
  5893.           return REG_ESPACE;
  5894.         ++paramc;
  5895.         params[backref_se].se = re_se_backref;
  5896.         params[backref_se].op1 = c1;
  5897.         side = (rx_side_effect)backref_se;
  5898.         goto add_side_effect;
  5899.           }
  5900.               break;
  5901.  
  5902.             case '+':
  5903.             case '?':
  5904.               if (syntax & RE_BK_PLUS_QM)
  5905.                 goto handle_plus;
  5906.               else
  5907.                 goto normal_backslash;
  5908.  
  5909.             default:
  5910.             normal_backslash:
  5911.               /* You might think it would be useful for \ to mean
  5912.                  not to translate; but if we don't translate it
  5913.                  it will never match anything.  */
  5914.               c = TRANSLATE (c);
  5915.               goto normal_char;
  5916.             }
  5917.           break;
  5918.  
  5919.  
  5920.     default:
  5921.         /* Expects the character in `c'.  */
  5922.     normal_char:
  5923.         {
  5924.           rx_Bitset cs = rx_cset(&rxb->rx);
  5925.           struct rexp_node * match = rx_mk_r_cset (&rxb->rx, cs);
  5926.           rx_Bitset it;
  5927.           if (!(cs && match))
  5928.         return REG_ESPACE;
  5929.           it = inverse_translation (rxb, validate_inv_tr,
  5930.                     inverse_translate, translate, c);
  5931.           rx_bitset_union (CHAR_SET_SIZE, cs, it);
  5932.           append = match;
  5933.  
  5934.         append_node:
  5935.           /* This genericly appends the rexp APPEND to *LAST_EXPRESSION
  5936.            * and then parses the next character normally.
  5937.            */
  5938.           if (*last_expression)
  5939.         {
  5940.           struct rexp_node * concat
  5941.             = rx_mk_r_concat (&rxb->rx, *last_expression, append);
  5942.           if (!concat)
  5943.             return REG_ESPACE;
  5944.           *last_expression = concat;
  5945.           last_expression = &concat->params.pair.right;
  5946.         }
  5947.           else
  5948.         *last_expression = append;
  5949.         }
  5950.     } /* switch (c) */
  5951.     } /* while p != pend */
  5952.  
  5953.   
  5954.   /* Through the pattern now.  */
  5955.  
  5956.   if (!COMPILE_STACK_EMPTY) 
  5957.     return REG_EPAREN;
  5958.  
  5959.       free (compile_stack.stack);
  5960.  
  5961.   orig_rexp = rexp;
  5962. #ifdef RX_DEBUG
  5963.   if (rx_debug_compile)
  5964.     {
  5965.       dbug_rxb = rxb;
  5966.       fputs ("\n\nCompiling ", stdout);
  5967.       fwrite (pattern, 1, size, stdout);
  5968.       fputs (":\n", stdout);
  5969.       rxb->se_params = params;
  5970.       print_rexp (&rxb->rx, orig_rexp, 2, re_seprint, stdout);
  5971.     }
  5972. #endif
  5973.   {
  5974.     rx_Bitset cs = rx_cset(&rxb->rx);
  5975.     rx_Bitset cs2 = rx_cset(&rxb->rx);
  5976.     char * se_map = (char *) alloca (paramc);
  5977.     struct rexp_node * new_rexp = 0;
  5978.     bzero (se_map, paramc);
  5979.     find_backrefs (se_map, rexp, params);
  5980.     
  5981.     fewer_side_effects =
  5982.       remove_unecessary_side_effects (&rxb->rx, se_map,
  5983.                       rx_copy_rexp (&rxb->rx, rexp), params);
  5984.     {
  5985.       char * syntax_parens = rxb->syntax_parens;
  5986.       if (syntax_parens == (char *)0x1)
  5987.     rexp = remove_unecessary_side_effects
  5988.       (&rxb->rx, se_map, rexp, params);
  5989.       else if (syntax_parens)
  5990.     {
  5991.       int x;
  5992.       for (x = 0; x < paramc; ++x)
  5993.         if ((   (params[x].se == re_se_lparen)
  5994.          || (params[x].se == re_se_rparen))
  5995.         && (!syntax_parens [params[x].op1]))
  5996.           se_map [x] = 1;
  5997.       rexp = remove_unecessary_side_effects
  5998.         (&rxb->rx, se_map, rexp, params);
  5999.     }
  6000.     }
  6001.  
  6002.     /* At least one more optimization would be nice to have here but i ran out 
  6003.      * of time.  The idea would be to delay side effects.  
  6004.      * For examle, `(abc)' is the same thing as `abc()' except that the
  6005.      * left paren is offset by 3 (which we know at compile time).
  6006.      * (In this comment, write that second pattern `abc(:3:)' 
  6007.      * where `(:3:' is a syntactic unit.)
  6008.      *
  6009.      * Trickier:  `(abc|defg)'  is the same as `(abc(:3:|defg(:4:))'
  6010.      * (The paren nesting may be hard to follow -- that's an alternation
  6011.      *    of `abc(:3:' and `defg(:4:' inside (purely syntactic) parens
  6012.      *  followed by the closing paren from the original expression.)
  6013.      *
  6014.      * Neither the expression tree representation nor the the nfa make
  6015.      * this very easy to write. :(
  6016.      */
  6017.  
  6018.     alloca (0);
  6019.   /* What we compile is different than what the parser returns.
  6020.    * Suppose the parser returns expression R.
  6021.    * Let R' be R with unnecessary register assignments removed 
  6022.    * (see REMOVE_UNECESSARY_SIDE_EFFECTS, above).
  6023.    *
  6024.    * What we will compile is the expression:
  6025.    *
  6026.    *    m{try}R{win}\|s{try}R'{win}
  6027.    *
  6028.    * {try} and {win} denote side effect epsilons (see EXPLORE_FUTURE).
  6029.    * 
  6030.    * When trying a match, we insert an `m' at the beginning of the 
  6031.    * string if the user wants registers to be filled, `s' if not.
  6032.    */
  6033.     new_rexp =
  6034.       rx_mk_r_alternate
  6035.     (&rxb->rx,
  6036.      rx_mk_r_concat
  6037.      (&rxb->rx,
  6038.       rx_mk_r_cset (&rxb->rx, cs),
  6039.       rx_mk_r_concat
  6040.       (&rxb->rx,
  6041.        rx_mk_r_concat
  6042.        (&rxb->rx,
  6043.         rx_mk_r_side_effect (&rxb->rx, (void*)re_se_try),
  6044.         fewer_side_effects),
  6045.        rx_mk_r_side_effect (&rxb->rx,(void*)re_se_win))),
  6046.      rx_mk_r_concat
  6047.      (&rxb->rx,
  6048.       rx_mk_r_cset (&rxb->rx, cs2),
  6049.       rx_mk_r_concat
  6050.       (&rxb->rx,
  6051.        rx_mk_r_concat
  6052.        (&rxb->rx,
  6053.         rx_mk_r_side_effect (&rxb->rx, (void*)re_se_try),
  6054.         rexp),
  6055.        rx_mk_r_side_effect (&rxb->rx,(void*)re_se_win))));
  6056.  
  6057.     if (!(new_rexp && cs && cs2))
  6058.       return REG_ESPACE;
  6059.     RX_bitset_enjoin (cs2, 'm'); /* prefixed to the rexp used for matching. */
  6060.     RX_bitset_enjoin (cs, 's'); /* prefixed to the rexp used for searching. */
  6061.     rexp = new_rexp;
  6062.   }
  6063.  
  6064. #ifdef RX_DEBUG
  6065.   if (rx_debug_compile)
  6066.     {
  6067.       fputs ("\n...which is compiled as:\n", stdout);
  6068.       print_rexp (&rxb->rx, rexp, 2, re_seprint, stdout);
  6069.     }
  6070. #endif
  6071.   {
  6072.     struct rx_nfa_state *start = 0;
  6073.     struct rx_nfa_state *end = 0;
  6074.  
  6075.     if (!rx_build_nfa (&rxb->rx, rexp, &start, &end))
  6076.       return REG_ESPACE;    /*  */
  6077.     else
  6078.       {
  6079.     void * mem = (void *)rxb->buffer;
  6080.     unsigned long size = rxb->allocated;
  6081.     int start_id;
  6082.     char * perm_mem;
  6083.     int iterator_size = paramc * sizeof (params[0]);
  6084.  
  6085.     end->is_final = 1;
  6086.     start->is_start = 1;
  6087.     rx_name_nfa_states (&rxb->rx);
  6088.     start_id = start->id;
  6089. #ifdef RX_DEBUG
  6090.     if (rx_debug_compile)
  6091.       {
  6092.         fputs ("...giving the NFA: \n", stdout);
  6093.         dbug_rxb = rxb;
  6094.         print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout);
  6095.       }
  6096. #endif
  6097.     if (!rx_eclose_nfa (&rxb->rx))
  6098.       return REG_ESPACE;
  6099.     else
  6100.       {
  6101.         rx_delete_epsilon_transitions (&rxb->rx);
  6102.         
  6103.         /* For compatability reasons, we need to shove the
  6104.          * compiled nfa into one chunk of malloced memory.
  6105.          */
  6106.         rxb->rx.reserved = (   sizeof (params[0]) * paramc
  6107.                 +  rx_sizeof_bitset (rxb->rx.local_cset_size));
  6108. #ifdef RX_DEBUG
  6109.         if (rx_debug_compile)
  6110.           {
  6111.         dbug_rxb = rxb;
  6112.         fputs ("...which cooks down (uncompactified) to: \n", stdout);
  6113.         print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout);
  6114.           }
  6115. #endif
  6116.         if (!rx_compactify_nfa (&rxb->rx, &mem, &size))
  6117.           return REG_ESPACE;
  6118.         rxb->buffer = mem;
  6119.         rxb->allocated = size;
  6120.         rxb->rx.buffer = mem;
  6121.         rxb->rx.allocated = size;
  6122.         perm_mem = ((char *)rxb->rx.buffer
  6123.             + rxb->rx.allocated - rxb->rx.reserved);
  6124.         rxb->se_params = ((struct re_se_params *)perm_mem);
  6125.         bcopy (params, rxb->se_params, iterator_size);
  6126.         perm_mem += iterator_size;
  6127.         rxb->fastset = (rx_Bitset) perm_mem;
  6128.         rxb->start = rx_id_to_nfa_state (&rxb->rx, start_id);
  6129.       }
  6130.     rx_bitset_null (rxb->rx.local_cset_size, rxb->fastset);
  6131.     rxb->can_match_empty = compute_fastset (rxb, orig_rexp);
  6132.     rxb->match_regs_on_stack =
  6133.       registers_on_stack (rxb, orig_rexp, 0, params); 
  6134.     rxb->search_regs_on_stack =
  6135.       registers_on_stack (rxb, fewer_side_effects, 0, params);
  6136.     if (rxb->can_match_empty)
  6137.       rx_bitset_universe (rxb->rx.local_cset_size, rxb->fastset);
  6138.     rxb->is_anchored = is_anchored (orig_rexp, (rx_side_effect) re_se_hat);
  6139.     rxb->begbuf_only = is_anchored (orig_rexp,
  6140.                     (rx_side_effect) re_se_begbuf);
  6141.       }
  6142.     rx_free_rexp (&rxb->rx, rexp);
  6143.     free (params);
  6144. #ifdef RX_DEBUG
  6145.     if (rx_debug_compile)
  6146.       {
  6147.     dbug_rxb = rxb;
  6148.     fputs ("...which cooks down to: \n", stdout);
  6149.     print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout);
  6150.       }
  6151. #endif
  6152.   }
  6153.   return REG_NOERROR;
  6154. }
  6155.  
  6156.  
  6157.  
  6158. /* This table gives an error message for each of the error codes listed
  6159.    in regex.h.  Obviously the order here has to be same as there.  */
  6160.  
  6161. const char * rx_error_msg[] =
  6162. { 0,                        /* REG_NOERROR */
  6163.     "No match",                    /* REG_NOMATCH */
  6164.     "Invalid regular expression",        /* REG_BADPAT */
  6165.     "Invalid collation character",        /* REG_ECOLLATE */
  6166.     "Invalid character class name",        /* REG_ECTYPE */
  6167.     "Trailing backslash",            /* REG_EESCAPE */
  6168.     "Invalid back reference",            /* REG_ESUBREG */
  6169.     "Unmatched [ or [^",            /* REG_EBRACK */
  6170.     "Unmatched ( or \\(",            /* REG_EPAREN */
  6171.     "Unmatched \\{",                /* REG_EBRACE */
  6172.     "Invalid content of \\{\\}",        /* REG_BADBR */
  6173.     "Invalid range end",            /* REG_ERANGE */
  6174.     "Memory exhausted",                /* REG_ESPACE */
  6175.     "Invalid preceding regular expression",    /* REG_BADRPT */
  6176.     "Premature end of regular expression",    /* REG_EEND */
  6177.     "Regular expression too big",        /* REG_ESIZE */
  6178.     "Unmatched ) or \\)",            /* REG_ERPAREN */
  6179. };
  6180.  
  6181.  
  6182.  
  6183. /* Test if at very beginning or at very end of the virtual concatenation
  6184.  *  of `string1' and `string2'.  If only one string, it's `string2'.  
  6185.  */
  6186.  
  6187. #define AT_STRINGS_BEG() \
  6188.   (string1 \
  6189.    ? ((tst_half == 0) \
  6190.       && ((unsigned char *)tst_pos == (unsigned char *)string1 - 1)) \
  6191.    : ((unsigned char *)tst_pos == (unsigned char *)string2 - 1))
  6192.  
  6193. #define AT_STRINGS_END() \
  6194.   (string2 \
  6195.    ? ((tst_half == 1) \
  6196.       && ((unsigned char *)tst_pos \
  6197.       == ((unsigned char *)string2 + size2 - 1))) \
  6198.    : ((unsigned char *)tst_pos == ((unsigned char *)string1 + size1 - 1)))
  6199.  
  6200. /* Test if D points to a character which is word-constituent.  We have
  6201.  * two special cases to check for: if past the end of string1, look at
  6202.  * the first character in string2; and if before the beginning of
  6203.  * string2, look at the last character in string1.
  6204.  *
  6205.  * Assumes `string1' exists, so use in conjunction with AT_STRINGS_BEG ().  
  6206.  */
  6207. #define LETTER_P(d)                            \
  6208.   (SYNTAX ((string2 && (tst_half == 0)                    \
  6209.         && ((d) == ((unsigned char *)string1 + size1)))        \
  6210.        ? *(unsigned char *)string2                    \
  6211.        : ((string1 && (tst_half == 1)                \
  6212.            && ((d) == (unsigned char *)string2 - 1))        \
  6213.           ? *((unsigned char *)string1 + size1 - 1)            \
  6214.           : *(d))) == Sword)
  6215.  
  6216. /* Test if the character at D and the one after D differ with respect
  6217.  * to being word-constituent.  
  6218.  */
  6219. #define AT_WORD_BOUNDARY(d)                        \
  6220.   (AT_STRINGS_BEG () || AT_STRINGS_END () || LETTER_P (d) != LETTER_P (d + 1))
  6221.  
  6222.  
  6223. static char slowmap [256] =
  6224. {
  6225.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6226.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6227.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6228.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6229.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6230.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6231.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6232.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6233.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6234.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6235.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6236.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6237.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6238.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6239.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6240.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6241. };
  6242.  
  6243. #ifdef __STDC__
  6244. static void
  6245. rx_blow_up_fastmap (struct re_pattern_buffer * rxb)
  6246. #else
  6247. static void
  6248. rx_blow_up_fastmap (rxb)
  6249.      struct re_pattern_buffer * rxb;
  6250. #endif
  6251. {
  6252.   int x;
  6253.   for (x = 0; x < 256; ++x)    /* &&&& 3.6 % */
  6254.     rxb->fastmap [x] = !!RX_bitset_member (rxb->fastset, x);
  6255.   rxb->fastmap_accurate = 1;
  6256. }
  6257.  
  6258.  
  6259.  
  6260.  
  6261. struct stack_chunk
  6262. {
  6263.   struct stack_chunk * next_chunk;
  6264.   int bytes_left;
  6265.   char * sp;
  6266. };
  6267.  
  6268. #define PUSH(CHUNK_VAR,BYTES)   \
  6269.   if (!CHUNK_VAR || (CHUNK_VAR->bytes_left < (BYTES)))  \
  6270.     {                    \
  6271.       struct stack_chunk * new_chunk;    \
  6272.       if (free_chunks)            \
  6273.     {                \
  6274.       new_chunk = free_chunks;    \
  6275.       free_chunks = free_chunks->next_chunk; \
  6276.     }                \
  6277.       else                \
  6278.     {                \
  6279.       new_chunk = (struct stack_chunk *)alloca (chunk_bytes); \
  6280.       if (!new_chunk)        \
  6281.         {                \
  6282.           ret_val = 0;        \
  6283.           goto test_do_return;    \
  6284.         }                \
  6285.     }                \
  6286.       new_chunk->sp = (char *)new_chunk + sizeof (struct stack_chunk); \
  6287.       new_chunk->bytes_left = chunk_bytes - (BYTES); \
  6288.       new_chunk->next_chunk = CHUNK_VAR; \
  6289.       CHUNK_VAR = new_chunk;        \
  6290.     } \
  6291.   else \
  6292.     (CHUNK_VAR->sp += (BYTES)), (CHUNK_VAR->bytes_left -= (BYTES))
  6293.  
  6294. #define POP(CHUNK_VAR,BYTES) \
  6295.   if (CHUNK_VAR->sp == ((char *)CHUNK_VAR + sizeof(*CHUNK_VAR))) \
  6296.     { \
  6297.       struct stack_chunk * new_chunk = CHUNK_VAR->next_chunk; \
  6298.       CHUNK_VAR->next_chunk = free_chunks; \
  6299.       free_chunks = CHUNK_VAR; \
  6300.       CHUNK_VAR = new_chunk; \
  6301.     } \
  6302.   else \
  6303.     (CHUNK_VAR->sp -= BYTES), (CHUNK_VAR->bytes_left += BYTES)
  6304.  
  6305. struct counter_frame
  6306. {
  6307.   int tag;
  6308.   int val;
  6309.   struct counter_frame * inherited_from; /* If this is a copy. */
  6310.   struct counter_frame * cdr;
  6311. };
  6312.  
  6313. struct backtrack_frame
  6314. {
  6315.   char * counter_stack_sp;
  6316.  
  6317.   /* A frame is used to save the matchers state when it crosses a 
  6318.    * backtracking point.  The `stk_' fields correspond to variables
  6319.    * in re_search_2 (just strip off thes `stk_').  They are documented
  6320.    * tere.
  6321.    */
  6322.   struct rx_superstate * stk_super;
  6323.   const unsigned char * stk_tst_pos;
  6324.   int stk_tst_half;
  6325.   unsigned int stk_c;
  6326.   const unsigned char * stk_tst_str_half;
  6327.   const unsigned char * stk_tst_end_half;
  6328.   int stk_last_l;
  6329.   int stk_last_r;
  6330.   int stk_test_ret;
  6331.  
  6332.   /* This is the list of options left to explore at the backtrack
  6333.    * point for which this frame was created. 
  6334.    */
  6335.   struct rx_distinct_future * df;
  6336.  
  6337. #ifdef RX_DEBUG
  6338.    int stk_line_no;
  6339. #endif
  6340. };
  6341.  
  6342.  
  6343.  
  6344. #ifdef __STDC__
  6345. int
  6346. re_search_2 (struct re_pattern_buffer *rxb,
  6347.          const char * string1, int size1,
  6348.          const char * string2, int size2,
  6349.          int startpos, int range,
  6350.          struct re_registers *regs,
  6351.          int stop)
  6352. #else
  6353. int
  6354. re_search_2 (rxb, string1, size1, string2, size2, startpos, range, regs, stop)
  6355.      struct re_pattern_buffer *rxb;
  6356.      const char * string1;
  6357.      int size1;
  6358.      const char * string2;
  6359.      int size2;
  6360.      int startpos;
  6361.      int range;
  6362.      struct re_registers *regs;
  6363.      int stop;
  6364. #endif
  6365. {
  6366.   /* Two groups of registers are kept.  The group with the register state
  6367.    * of the current test match, and the group that holds the state at the end
  6368.    * of the best known match, if any.
  6369.    *
  6370.    * For some patterns, there may also be registers saved on the stack.
  6371.    */
  6372.   regoff_t * lparen = 0; /* scratch space for register returns */
  6373.   regoff_t * rparen = 0;
  6374.   regoff_t * best_lpspace = 0; /* in case the user doesn't want these */
  6375.   regoff_t * best_rpspace = 0; /* values, we still need space to store
  6376.                 * them.  Normally, this memoryis unused
  6377.                 * and the space pointed to by REGS is 
  6378.                 * used instead.
  6379.                 */
  6380.   
  6381.   int last_l;            /* Highest index of a valid lparen. */
  6382.   int last_r;            /* It's dual. */
  6383.  
  6384.   int * best_lparen;        /* This contains the best known register */
  6385.   int * best_rparen;        /* assignments. 
  6386.                  * This may point to the same mem as
  6387.                  * best_lpspace, or it might point to memory
  6388.                  * passed by the caller.
  6389.                  */
  6390.   int best_last_l;        /* best_last_l:best_lparen::last_l:lparen */
  6391.   int best_last_r;
  6392.   
  6393.   
  6394.  
  6395.   /* Figure the number of registers we may need for use in backreferences.
  6396.    * The number here includes an element for register zero.  
  6397.    */
  6398.   unsigned num_regs = rxb->re_nsub + 1;
  6399.  
  6400.   int total_size = size1 + size2;
  6401.  
  6402.  
  6403.   /***** INIT re_search_2 */
  6404.   
  6405.   /* Check for out-of-range STARTPOS.  */
  6406.   if ((startpos < 0) || (startpos > total_size))
  6407.     return -1;
  6408.  
  6409.   /* Fix up RANGE if it might eventually take us outside
  6410.    * the virtual concatenation of STRING1 and STRING2.
  6411.    */
  6412.   {
  6413.     int endpos = startpos + range;
  6414.     if (endpos < -1)
  6415.       range = (-1 - startpos);
  6416.     else if (endpos > total_size)
  6417.       range = total_size - startpos;
  6418.   }
  6419.  
  6420.   /* If the search isn't to be a backwards one, don't waste time in a
  6421.    * long search for a pattern that says it is anchored.
  6422.    */
  6423.   if (rxb->begbuf_only && (range > 0))
  6424.     {
  6425.       if (startpos > 0)
  6426.     return -1;
  6427.       else
  6428.     range = 1;
  6429.     }
  6430.  
  6431.   /* Then, decide whether to use internal or user-provided reg buffers. */
  6432.   if (!regs || rxb->no_sub)
  6433.     {
  6434.       best_lpspace = (regoff_t *)REGEX_ALLOCATE (num_regs * sizeof(regoff_t));
  6435.       best_rpspace = (regoff_t *)REGEX_ALLOCATE (num_regs * sizeof(regoff_t));
  6436.       best_lparen = best_lpspace;
  6437.       best_rparen = best_rpspace;
  6438.     }
  6439.   else
  6440.     {    
  6441.       /* Have the register data arrays been allocated?  */
  6442.       if (rxb->regs_allocated == REGS_UNALLOCATED)
  6443.     { /* No.  So allocate them with malloc.  We need one
  6444.          extra element beyond `num_regs' for the `-1' marker
  6445.          GNU code uses.  */
  6446.       regs->num_regs = MAX (RE_NREGS, rxb->re_nsub + 1);
  6447.       regs->start = TALLOC (regs->num_regs, regoff_t);
  6448.       regs->end = TALLOC (regs->num_regs, regoff_t);
  6449.       if (regs->start == 0 || regs->end == 0)
  6450.         return -2;
  6451.       rxb->regs_allocated = REGS_REALLOCATE;
  6452.     }
  6453.       else if (rxb->regs_allocated == REGS_REALLOCATE)
  6454.     { /* Yes.  If we need more elements than were already
  6455.          allocated, reallocate them.  If we need fewer, just
  6456.          leave it alone.  */
  6457.       if (regs->num_regs < num_regs + 1)
  6458.         {
  6459.           regs->num_regs = num_regs + 1;
  6460.           RETALLOC (regs->start, regs->num_regs, regoff_t);
  6461.           RETALLOC (regs->end, regs->num_regs, regoff_t);
  6462.           if (regs->start == 0 || regs->end == 0)
  6463.         return -2;
  6464.         }
  6465.     }
  6466.       else if (rxb->regs_allocated != REGS_FIXED)
  6467.     return -2;
  6468.  
  6469.       if (regs->num_regs < num_regs + 1)
  6470.     {
  6471.       best_lpspace = ((regoff_t *)
  6472.               REGEX_ALLOCATE (num_regs * sizeof(regoff_t)));
  6473.       best_rpspace = ((regoff_t *)
  6474.               REGEX_ALLOCATE (num_regs * sizeof(regoff_t)));
  6475.       best_lparen = best_lpspace;
  6476.       best_rparen = best_rpspace;
  6477.     }
  6478.       else
  6479.     {
  6480.       best_lparen = regs->start;
  6481.       best_rparen = regs->end;
  6482.     }
  6483.     }
  6484.   
  6485.   lparen = (regoff_t *) REGEX_ALLOCATE (num_regs * sizeof(regoff_t));
  6486.   rparen = (regoff_t *) REGEX_ALLOCATE (num_regs * sizeof(regoff_t)); 
  6487.   
  6488.   if (!(best_rparen && best_lparen && lparen && rparen))
  6489.     return -2;
  6490.   
  6491.   best_last_l = best_last_r = -1;
  6492.  
  6493.  
  6494.  
  6495.   /***** fastmap/search loop, initialization */
  6496.  
  6497.   /* This is the loop that scans using the fastmap, and sometimes tries to 
  6498.    * match. From this point on, don't return.  Instead, assign to ret_val
  6499.    * and goto fail.
  6500.    */
  6501.   {
  6502.     const char * translate = rxb->translate ? rxb->translate : id_translation;
  6503.  
  6504.     /** This is state associated with returning to the caller. */
  6505.  
  6506.     int ret_val = -1;
  6507.  
  6508.     /*   A sentinal is sometimes installed in the fastmap.  This records
  6509.      *   where so it can be removed before returning.
  6510.      */
  6511.     int fastmap_chr = -1;
  6512.     int fastmap_val;
  6513.  
  6514.     /** End of state associated with returning to the caller. */
  6515.  
  6516.     /** Start of variables associated with the fastmap based search: */
  6517.  
  6518.     char * fastmap = rxb->fastmap ? (char *)rxb->fastmap : (char *)slowmap;
  6519.     int search_direction;    /* 1 or -1 */
  6520.     int search_end;        /* first position to not try */
  6521.     int offset;            /* either size1 or 0 as string == string2 */
  6522.  
  6523.     /* The string-pair position of the fastmap/search loop: */
  6524.     const unsigned char * pos;    /* The current pos. */
  6525.     const unsigned char * string; /* The current string half. */
  6526.     const unsigned char * end;    /* End of current string. */
  6527.     int size;            /* Current string's size */
  6528.     int half;            /* 0 means string1, 1 means string2 */
  6529.  
  6530.     /** End of variables associated with the fastmap based search: */
  6531.  
  6532.  
  6533.     /** Start of variables associated with trying a match
  6534.      *  after the fastmap has found a plausible starting point.
  6535.      */
  6536.  
  6537.     struct rx_superstate * start_super = 0; /* The superNFA start state. */
  6538.  
  6539.     /* Two nfa's were compiled.  `m' is complete, `s' faster. */
  6540.     int nfa_choice = ((regs && !rxb->least_subs) ? 'm' : 's');
  6541.  
  6542.     const unsigned char * abs_end; /* Don't fetch a character from here. */
  6543.     int first_found;        /* If true, return after finding any match. */
  6544.  
  6545.     /** End of variables associated with trying a match. */
  6546.  
  6547.  
  6548.     /* Update the fastmap now if not correct already. 
  6549.      * When the regexp was compiled, the fastmap was computed
  6550.      * and stored in a bitset.  This expands the bitset into a
  6551.      * character array containing 1s and 0s.
  6552.      */
  6553.     if ((fastmap == rxb->fastmap) && !rxb->fastmap_accurate)
  6554.       rx_blow_up_fastmap (rxb);
  6555.  
  6556.     /* Now we build the starting state of the supernfa. */
  6557.     {
  6558.       struct rx_superset * start_contents;
  6559.       struct rx_nfa_state_set * start_nfa_set;
  6560.       
  6561.       /* We presume here that the nfa start state has only one
  6562.        * possible future with no side effects.  
  6563.        */
  6564.       start_nfa_set = rxb->start->futures->destset;
  6565.       start_contents =
  6566.     rx_superstate_eclosure_union (&rxb->rx,
  6567.                       rx_superset_cons (&rxb->rx, 0, 0),
  6568.                       start_nfa_set);
  6569.       
  6570.       if (!start_contents)
  6571.     return -1;
  6572.       rx_protect_superset (&rxb->rx, start_contents);
  6573.       start_super = rx_superstate (&rxb->rx, start_contents);
  6574.       if (!start_super)
  6575.     return -1;
  6576.       rx_lock_superstate (&rxb->rx, start_super);
  6577.       rx_release_superset (&rxb->rx, start_contents);
  6578.     }
  6579.     
  6580.     /* This computes an upper bound on string addresses for use by
  6581.      * the match-test.
  6582.      */
  6583.     abs_end = ((const unsigned char *) ((stop <= size1)
  6584.                     ? string1 + stop
  6585.                     : string2 + stop - size1));
  6586.  
  6587.     /* We have the option to look for the best match or the first
  6588.      * one we can find.  If the user isn't asking for register information,
  6589.      * we don't need to find the best match.
  6590.      */
  6591.     first_found = !regs;
  6592.  
  6593.     /* Compute search_end & search_direction for the fastmap loop. */
  6594.     if (range >= 0)
  6595.       {
  6596.     search_end = MIN (size1 + size2, startpos + range) + 1;
  6597.     search_direction = 1;
  6598.       }
  6599.     else
  6600.       {
  6601.     search_end = MAX(-1, startpos + range);
  6602.     search_direction = -1;
  6603.       }
  6604.  
  6605.     /* The vacuous search always turns up nothing. */
  6606.     if ((search_direction == 1)
  6607.     ? (startpos > search_end)
  6608.     : (startpos < search_end))
  6609.       return -1;
  6610.  
  6611.     /* Set string/size/offset/end -- the state that tells the fastmap
  6612.      * loop which half of the string we're in.  Also set pos, which
  6613.      * is the addr of the current fastmap scan position.
  6614.      */
  6615.     if (!string2 || (startpos < size1))
  6616.       {
  6617.     string = (const unsigned char *)string1;
  6618.     size = size1;
  6619.     offset = 0;
  6620.     pos = (const unsigned char *)(string1 + startpos);
  6621.     half = 0;
  6622.     end = (const unsigned char *)MIN(string1 + size1, string1 + stop);
  6623.       }
  6624.     else
  6625.       {
  6626.     string = (const unsigned char *)string2;
  6627.     size = size2;
  6628.     offset = size1;
  6629.     pos = (const unsigned char *)(string2 + startpos - size1);
  6630.     half = 1;
  6631.     end = (const unsigned char *)MIN(string2 + size2,
  6632.                      string2 + stop - size1);
  6633.       }
  6634.  
  6635.  
  6636.  
  6637.  
  6638.     /***** fastmap/search loop,  body */
  6639.  
  6640.  
  6641.   init_fastmap_sentinal:
  6642.  
  6643.     /* For the sake of fast fastmapping, set a sentinal in the fastmap.
  6644.      * This sentinal will trap the fastmap loop when it reaches the last
  6645.      * valid character in a string half.
  6646.      *
  6647.      * This must be reset when the fastmap/search loop crosses a string 
  6648.      * boundry, and before returning to the caller.  So sometimes,
  6649.      * the fastmap loop is restarted with `continue', othertimes by
  6650.      * `goto init_fastmap_sentinal'.
  6651.      */
  6652.     if (size)
  6653.       {
  6654.     fastmap_chr = ((search_direction == 1)
  6655.                ? *(end - 1)
  6656.                : *string);
  6657.     fastmap_val = fastmap[fastmap_chr];
  6658.     fastmap[fastmap_chr] = 1;
  6659.       }
  6660.     else
  6661.       fastmap_chr = -1;
  6662.  
  6663.     do
  6664.       {
  6665.     /* If we haven't reached the end of a string half, and if the
  6666.      * pattern can't match the empty string, then the fastmap 
  6667.      * optimization applies.  This conditional scans using the 
  6668.      * fastmap -- stoping when a string half ends, or when a 
  6669.      * plausible starting point for a match is found.
  6670.      * It updates HIT_BOUND to tell which case occured.
  6671.      */
  6672.     if (pos == end)
  6673.       goto fastmap_hit_bound;
  6674.     else
  6675.       {
  6676.         if (search_direction == 1)
  6677.           {
  6678.         for (;;)
  6679.           {
  6680.             while (!fastmap[*pos])
  6681.               ++pos;
  6682.             if ((*pos != fastmap_chr) || fastmap_val)
  6683.               goto commence_a_matchin;
  6684.             else 
  6685.               {
  6686.             ++pos;
  6687.             if (pos == end)
  6688.               goto fastmap_hit_bound;
  6689.               }
  6690.           }
  6691.           }
  6692.         else
  6693.           {
  6694.         const unsigned char * bound = string - 1;
  6695.         for (;;)
  6696.           {
  6697.             while (!fastmap[*pos])
  6698.               --pos;
  6699.             if ((*pos != fastmap_chr) || fastmap_val)
  6700.               goto commence_a_matchin;
  6701.             else 
  6702.               {
  6703.             --pos;
  6704.             if (pos == bound)
  6705.               goto fastmap_hit_bound;
  6706.               }
  6707.           }
  6708.           }
  6709.       }
  6710.     
  6711.       fastmap_hit_bound:
  6712.     {
  6713.       /* If we hit a bound, it may simply be time to switch sides
  6714.        * between strings.
  6715.        */
  6716.       if ((search_direction == 1) && string2 && (half == 0))
  6717.         {
  6718.           string = (const unsigned char *)string2;
  6719.           size = size2;
  6720.           offset = size1;
  6721.           half = 1;
  6722.           end = (const unsigned char *)MIN(string2 + size2,
  6723.                            string2 + stop - size1);
  6724.           startpos = size1;
  6725.           pos = (const unsigned char *)string2;
  6726.           goto init_fastmap_sentinal;
  6727.         }
  6728.       else if (   string1
  6729.            && (search_direction == -1)
  6730.            && (half == 1))
  6731.         {
  6732.           string = (const unsigned char *)string1;
  6733.           size = size1;
  6734.           offset = 0;
  6735.           end = (const unsigned char *)string1 + size1;
  6736.           half = 0;
  6737.           startpos = size1 - 1;
  6738.           pos = (const unsigned char *)string1 + size1 - 1;
  6739.           goto init_fastmap_sentinal;
  6740.         }
  6741.       /* ...not a string split, simply no more string. 
  6742.        *
  6743.        * When searching backward, running out of string
  6744.        * is reason to quit.
  6745.        */
  6746.       else if (search_direction == -1)
  6747.         goto finish;
  6748.       
  6749.       /* ...when searching forward, we allow the possibility
  6750.        * of an (empty) match after the last character in the
  6751.        * virtual string.  So, fall through to the matcher
  6752.        */
  6753.     }
  6754.  
  6755.  
  6756.       commence_a_matchin:
  6757.  
  6758.     /***** fastmap/search loop body
  6759.      *          test for a match that begins at pos
  6760.      */
  6761.  
  6762.     /* Now the fastmap loop has brought us to a plausible 
  6763.      * starting point for a match.  So, it's time to run the
  6764.      * NFA and see if a match occured.
  6765.      */
  6766.  
  6767.     startpos = pos - string + offset;
  6768.     if (startpos == search_end)
  6769.       goto finish;
  6770.     
  6771.     last_l = last_r = 0;
  6772.     lparen[0] = startpos;    /* We know match-begin for this test... */
  6773.  
  6774.     /* The test matcher is essentially a recursive function
  6775.      * that does an exhaustive run of the superNFA at the 
  6776.      * test position.  For performance, that function has 
  6777.      * been in-lined by hand.
  6778.      */
  6779.  
  6780. #ifndef HAVE_GNUC_LABELS
  6781. #define CASE(A,B)    case A:
  6782. #else
  6783. #define CASE(A,B)    case A: B:
  6784.       static void * rx_labels_instruction_table[] =
  6785.         {
  6786.           [rx_backtrack_point] &&backtrack_point,
  6787.           [rx_backtrack] &&backtrack,
  6788.           [rx_do_side_effects] &&do_side_effects,
  6789.           [rx_cache_miss] &&cache_miss,
  6790.           [rx_next_char] 0,
  6791.           [rx_error_inx] 0
  6792.         };
  6793. #endif
  6794.     {      
  6795.       /* The current superNFA position of the matcher. */
  6796.       struct rx_superstate * super = start_super;
  6797.       
  6798.       /* The matcher interprets a series of instruction frames.
  6799.        * This is the `instruction counter' for the interpretation.
  6800.        */
  6801.       struct rx_inx * ifr;
  6802.       
  6803.       /* We insert a ghost character in the string to prime
  6804.        * the nfa.  tst_pos, tst_str_half, and tst_end_half
  6805.        * keep track of the test-match position and string-half.
  6806.        */
  6807.       const unsigned char * tst_pos = pos - 1;
  6808.       int tst_half = half;
  6809.       unsigned char c = nfa_choice;
  6810.       
  6811.       const unsigned char * tst_str_half = string;
  6812.       const unsigned char * tst_end_half = end;
  6813.       
  6814.       struct stack_chunk * counter_stack = 0;
  6815.       struct stack_chunk * backtrack_stack = 0;
  6816.       int backtrack_frame_bytes =
  6817.         (sizeof (struct backtrack_frame)
  6818.          + (rxb->match_regs_on_stack
  6819.         ? sizeof (regoff_t) * (num_regs + 1) * 2
  6820.         : 0));
  6821.       int chunk_bytes = backtrack_frame_bytes * 64;
  6822.       struct stack_chunk * free_chunks = 0;
  6823.  
  6824. #ifdef RX_DEBUG
  6825.       int backtrack_depth = 0;
  6826. #endif
  6827.  
  6828.       /* To return from this function, set test_ret and 
  6829.        * `goto test_do_return'.
  6830.        *
  6831.        * Possible return values are:
  6832.        *     1   --- end of string while the superNFA is still going
  6833.        *     0   --- internal error (out of memory)
  6834.        *    -1   --- search completed by reaching the superNFA fail state
  6835.        *    -2   --- a match was found, maybe not the longest.
  6836.        *
  6837.        * When the search is complete (-1), best_last_r indicates whether
  6838.        * a match was found.
  6839.        *
  6840.        * -2 is return only if first_found is non-zero.
  6841.        *
  6842.        * if first_found is non-zero, a return of -1 indicates no match,
  6843.        * otherwise, best_last_r has to be checked.
  6844.        */
  6845.       int test_ret = -1;
  6846.       
  6847.       while (1)
  6848.         {
  6849.           int inx;
  6850. #ifdef RX_DEBUG
  6851.           /* There is a search tree with every node as set of deterministic
  6852.            * transitions in the super nfa.  For every branch of a 
  6853.            * backtrack point is an edge in the tree.
  6854.            * This counts up a pre-order of nodes in that tree.
  6855.            * It's saved on the search stack and printed when debugging. 
  6856.            */
  6857.           int line_no = 0;
  6858.           int lines_found = 0;
  6859. #endif
  6860.  
  6861.  
  6862.         top_of_cycle:
  6863.           /* A superstate is basicly a transition table, indexed by 
  6864.            * characters from the string being tested, and containing 
  6865.            * RX_INX structures.
  6866.            */
  6867.           ifr = &super->transitions [c];
  6868.           
  6869.         recurse_test_match:
  6870.           /* This is the point to which control is sent when the
  6871.            * test matcher recurses.  Before jumping here, some variables
  6872.            * need to be saved on the stack and setup for the recursion.
  6873.            */
  6874.  
  6875.         restart:
  6876.           /* Some instructions don't advance the matcher, but just
  6877.            * carry out some side effects and fetch a new instruction.
  6878.            * To dispatch that new instruction, `goto restart'.
  6879.            */
  6880.           
  6881.           {
  6882.         struct rx_inx * next_tr_table = (struct rx_inx *)ifr->data;
  6883.         struct rx_inx * this_tr_table = super->transitions;
  6884.         /* The fastest route through the loop is when the instruction 
  6885.          * is RX_NEXT_CHAR.  This case is detected when IFR->DATA
  6886.          * is non-zero.  In that case, it points to the next
  6887.          * superstate. 
  6888.          *
  6889.          * This allows us to not bother fetching the bytecode.
  6890.          */
  6891.         while (next_tr_table)
  6892.           {
  6893. #ifdef RX_DEBUG
  6894.             if (rx_debug_trace)
  6895.               {
  6896.             struct rx_superset * setp;
  6897.  
  6898.             fprintf (stderr, "%d %d>> re_next_char @ %d (%d)",
  6899.                  line_no,
  6900.                  backtrack_depth,
  6901.                  (tst_pos - tst_str_half
  6902.                   + (tst_half == 0
  6903.                      ? 0 : size1)), c);
  6904.             
  6905.             super =
  6906.               ((struct rx_superstate *)
  6907.                ((char *)this_tr_table
  6908.                 - ((unsigned long)
  6909.                    ((struct rx_superstate *)0)->transitions)));
  6910.  
  6911.             setp = super->contents;
  6912.             fprintf (stderr, "   superstet (rx=%d, &=%x: ",
  6913.                  rxb->rx.rx_id, setp);
  6914.             while (setp)
  6915.               {
  6916.                 fprintf (stderr, "%d ", setp->id);
  6917.                 setp = setp->cdr;
  6918.               }
  6919.             fprintf (stderr, "\n");
  6920.               }
  6921. #endif
  6922.             this_tr_table = next_tr_table;
  6923.             ++tst_pos;
  6924.             if (tst_pos == tst_end_half)
  6925.               {
  6926.             if (   (tst_pos != abs_end)
  6927.                 && string2
  6928.                 && half == 0)
  6929.               {
  6930.                 /* Here we are crossing the break 
  6931.                  * in a split string. 
  6932.                  */
  6933.                 tst_str_half = (const unsigned char *)string2;
  6934.                 tst_end_half = abs_end;
  6935.                 tst_pos = (const unsigned char *)string2;
  6936.                 tst_half = 1;
  6937.               }
  6938.             else
  6939.               {
  6940.                 test_ret = 1;
  6941.                 goto test_do_return;
  6942.               }
  6943.               }
  6944.             c = *tst_pos;
  6945.             ifr = this_tr_table + c;
  6946.             next_tr_table = (struct rx_inx *)ifr->data;
  6947.           }
  6948.         
  6949.         /* Here when we ran out cached next-char transitions. 
  6950.          * So, it will be necessary to do a more expensive
  6951.          * dispatch on the current instruction.  The superstate
  6952.          * pointer is allowed to become invalid during next-char
  6953.          * transitions -- now we must bring it up to date.
  6954.          */
  6955.         super =
  6956.           ((struct rx_superstate *)
  6957.            ((char *)this_tr_table
  6958.             - ((unsigned long)
  6959.                ((struct rx_superstate *)0)->transitions)));
  6960.           }
  6961.           
  6962.           /* We've encountered an instruction other than next-char.
  6963.            * Dispatch that instruction:
  6964.            */
  6965.           inx = (int)ifr->inx;
  6966. #ifdef HAVE_GNUC_LABELS
  6967.           goto *rx_labels_instruction_table[inx];
  6968. #endif
  6969. #ifdef RX_DEBUG
  6970.           if (rx_debug_trace)
  6971.         {
  6972.           struct rx_superset * setp = super->contents;
  6973.           
  6974.           fprintf (stderr, "%d %d>> %s @ %d (%d)", line_no,
  6975.                backtrack_depth,
  6976.                inx_names[inx],
  6977.                (tst_pos - tst_str_half
  6978.                 + (tst_half == 0 ? 0 : size1)), c);
  6979.           
  6980.           fprintf (stderr, "   superstet (rx=%d, &=%x: ",
  6981.                rxb->rx.rx_id, setp);
  6982.           while (setp)
  6983.             {
  6984.               fprintf (stderr, "%d ", setp->id);
  6985.               setp = setp->cdr;
  6986.             }
  6987.           fprintf (stderr, "\n");
  6988.         }
  6989. #endif
  6990.           switch ((enum rx_opcode)inx)
  6991.         {
  6992.         CASE(rx_do_side_effects,do_side_effects)
  6993.  
  6994.           /*  RX_DO_SIDE_EFFECTS occurs when we cross epsilon 
  6995.            *  edges associated with parentheses, backreferencing, etc.
  6996.            */
  6997.           {
  6998.             struct rx_distinct_future * df =
  6999.               (struct rx_distinct_future *)ifr->data_2;
  7000.             struct rx_se_list * el = df->effects;
  7001.             /* Side effects come in lists.  This walks down
  7002.              * a list, dispatching.
  7003.              */
  7004.             while (el)
  7005.               {
  7006. #ifdef HAVE_GNUC_LABELS
  7007.             static void * se_labels[] =
  7008.               {
  7009.                 [-re_se_try] &&se_try,
  7010.                 [-re_se_pushback] &&se_pushback,
  7011.                 [-re_se_push0] &&se_push0,
  7012.                 [-re_se_pushpos] &&se_pushpos,
  7013.                 [-re_se_chkpos] &&se_chkpos,
  7014.                 [-re_se_poppos] &&se_poppos,
  7015. #ifdef emacs
  7016.                 [-re_se_at_dot] &&se_at_dot,
  7017.                 [-re_se_syntax] &&se_syntax,
  7018.                 [-re_se_not_syntax] &&se_not_syntax,
  7019. #endif
  7020.                 [-re_se_begbuf] &&se_begbuf,
  7021.                 [-re_se_hat] &&se_hat,
  7022.                 [-re_se_wordbeg] &&se_wordbeg,
  7023.                 [-re_se_wordbound] &&se_wordbound,
  7024.                 [-re_se_notwordbound] &&se_notwordbound,
  7025.                 [-re_se_wordend] &&se_wordend,
  7026.                 [-re_se_endbuf] &&se_endbuf,
  7027.                 [-re_se_dollar] &&se_dollar,
  7028.                 [-re_se_fail] &&se_fail,
  7029.                 [-re_se_win] &&se_win
  7030.               };
  7031.             static void * se_lables2[] =
  7032.               {
  7033.                 [re_se_lparen] &&se_lparen,
  7034.                 [re_se_rparen] &&se_rparen,
  7035.                 [re_se_backref] &&se_backref,
  7036.                 [re_se_iter] &&se_iter,
  7037.                 [re_se_end_iter] &&se_end_iter
  7038.               };
  7039. #endif
  7040.             int effect = (int)el->car;
  7041.             if (effect < 0)
  7042.               {
  7043. #ifdef HAVE_GNUC_LABELS
  7044.                 goto *se_labels[-effect];
  7045. #endif
  7046. #ifdef RX_DEBUG
  7047.                 if (rx_debug_trace)
  7048.                   {
  7049.                 struct rx_superset * setp = super->contents;
  7050.                 
  7051.                 fprintf (stderr, "....%d %d>> %s\n", line_no,
  7052.                      backtrack_depth,
  7053.                      efnames[-effect]);
  7054.                   }
  7055. #endif
  7056.                 switch ((enum re_side_effects) effect)
  7057.                   {
  7058.                   CASE(re_se_pushback,se_pushback)
  7059.                 ifr = &df->future_frame;
  7060.                 if (!ifr->data)
  7061.                   {
  7062.                     struct rx_superstate * sup = super;
  7063.                     rx_lock_superstate (rx, sup);
  7064.                     if (!rx_handle_cache_miss (&rxb->rx,
  7065.                                    super, c,
  7066.                                    ifr->data_2))
  7067.                       {
  7068.                     rx_unlock_superstate (rx, sup);
  7069.                     test_ret = 0;
  7070.                     goto test_do_return;
  7071.                       }
  7072.                     rx_unlock_superstate (rx, sup);
  7073.                   }
  7074.                 /* --tst_pos; */
  7075.                 c = 't';
  7076.                 super
  7077.                   = ((struct rx_superstate *)
  7078.                      ((char *)ifr->data
  7079.                       - (long)(((struct rx_superstate *)0)
  7080.                            ->transitions)));
  7081.                 goto top_of_cycle;
  7082.                 break;
  7083.                   CASE(re_se_push0,se_push0)
  7084.                 {
  7085.                   struct counter_frame * old_cf
  7086.                      = (counter_stack
  7087.                     ? ((struct counter_frame *)
  7088.                        counter_stack->sp)
  7089.                     : 0);
  7090.                   struct counter_frame * cf;
  7091.                   PUSH (counter_stack,
  7092.                     sizeof (struct counter_frame));
  7093.                   cf = ((struct counter_frame *)
  7094.                     counter_stack->sp);
  7095.                   cf->tag = re_se_iter;
  7096.                   cf->val = 0;
  7097.                   cf->inherited_from = 0;
  7098.                   cf->cdr = old_cf;
  7099.                   break;
  7100.                 }
  7101.                   CASE(re_se_fail,se_fail)
  7102.                 goto test_do_return;
  7103.                   CASE(re_se_begbuf,se_begbuf)
  7104.                 if (!AT_STRINGS_BEG ())
  7105.                   goto test_do_return;
  7106.                 break;
  7107.                   CASE(re_se_endbuf,se_endbuf)
  7108.                 if (!AT_STRINGS_END ())
  7109.                   goto test_do_return;
  7110.                 break;
  7111.                   CASE(re_se_wordbeg,se_wordbeg)
  7112.                 if (   LETTER_P (tst_pos + 1)
  7113.                     && (   AT_STRINGS_BEG()
  7114.                     || !LETTER_P (tst_pos)))
  7115.                   break;
  7116.                 else
  7117.                   goto test_do_return;
  7118.                   CASE(re_se_wordend,se_wordend)
  7119.                 if (   !AT_STRINGS_BEG ()
  7120.                     && LETTER_P (tst_pos)
  7121.                     && (AT_STRINGS_END ()
  7122.                     || !LETTER_P (tst_pos + 1)))
  7123.                   break;
  7124.                 else
  7125.                   goto test_do_return;
  7126.                   CASE(re_se_wordbound,se_wordbound)
  7127.                 if (AT_WORD_BOUNDARY (tst_pos))
  7128.                   break;
  7129.                 else
  7130.                   goto test_do_return;
  7131.                   CASE(re_se_notwordbound,se_notwordbound)
  7132.                 if (!AT_WORD_BOUNDARY (tst_pos))
  7133.                   break;
  7134.                 else
  7135.                   goto test_do_return;
  7136.                   CASE(re_se_hat,se_hat)
  7137.                 if (AT_STRINGS_BEG ())
  7138.                   {
  7139.                     if (rxb->not_bol)
  7140.                       goto test_do_return;
  7141.                     else
  7142.                       break;
  7143.                   }
  7144.                 else
  7145.                   {
  7146.                     char pos_c = *tst_pos;
  7147.                     if (   (TRANSLATE (pos_c)
  7148.                         == TRANSLATE('\n'))
  7149.                     && rxb->newline_anchor)
  7150.                       break;
  7151.                     else
  7152.                       goto test_do_return;
  7153.                   }
  7154.                   CASE(re_se_dollar,se_dollar)
  7155.                 if (AT_STRINGS_END ())
  7156.                   {
  7157.                     if (rxb->not_eol)
  7158.                       goto test_do_return;
  7159.                     else
  7160.                       break;
  7161.                   }
  7162.                 else
  7163.                   {
  7164.                     const unsigned char * next_pos
  7165.                       = ((string2 && (tst_half == 0) &&
  7166.                       (tst_pos
  7167.                        == ((unsigned char *)
  7168.                            string1 + size1 - 1)))
  7169.                      ? (unsigned char *)string2
  7170.                      : tst_pos + 1);
  7171.                     
  7172.                     if (   (TRANSLATE (*next_pos)
  7173.                         == TRANSLATE ('\n'))
  7174.                     && rxb->newline_anchor)
  7175.                       break;
  7176.                     else
  7177.                       goto test_do_return;
  7178.                   }
  7179.                 
  7180.                   CASE(re_se_win,se_win)
  7181.                 /* This side effect indicates that we've 
  7182.                  * found a match, though not necessarily the 
  7183.                  * best match.  This is a fancy assignment to 
  7184.                  * register 0 unless the caller didn't 
  7185.                  * care about registers.  In which case,
  7186.                  * this stops the match.
  7187.                  */
  7188.                 {
  7189.                   int urhere =
  7190.                     ((int)(tst_pos - tst_str_half)
  7191.                      + ((tst_half == 0)
  7192.                     ? 0 : size1));
  7193.                   
  7194.                   /* This enforces `longest' */
  7195.                   if ((best_last_r < 0)
  7196.                       || (urhere + 1 > best_rparen[0]))
  7197.                     {
  7198.                       /* Record the best known and keep
  7199.                        * looking.
  7200.                        */
  7201.                       int x;
  7202.                       for (x = 0; x <= last_l; ++x)
  7203.                     best_lparen[x] = lparen[x];
  7204.                       best_last_l = last_l;
  7205.                       for (x = 0; x <= last_r; ++x)
  7206.                     best_rparen[x] = rparen[x];
  7207.                       best_rparen[0] = urhere + 1;
  7208.                       best_last_r = last_r;
  7209.                     }
  7210.                   /* If we're not reporting the match-length 
  7211.                    * or other register info, we need look no
  7212.                    * further.
  7213.                    */
  7214.                   if (first_found)
  7215.                     {
  7216.                       test_ret = -2;
  7217.                       goto test_do_return;
  7218.                     }
  7219.                 }
  7220.                 break;
  7221.                   CASE(re_se_try,se_try)
  7222.                 /* This is the first side effect in every
  7223.                  * expression.
  7224.                  *
  7225.                  *  FOR NO GOOD REASON...get rid of it...
  7226.                  */
  7227.                 break;
  7228.                   CASE(re_se_pushpos,se_pushpos)
  7229.                 {
  7230.                   int urhere =
  7231.                     ((int)(tst_pos - tst_str_half)
  7232.                      + ((tst_half == 0) ? 0 : size1));
  7233.                   struct counter_frame * old_cf
  7234.                     = (counter_stack
  7235.                        ? ((struct counter_frame *)
  7236.                       counter_stack->sp)
  7237.                        : 0);
  7238.                   struct counter_frame * cf;
  7239.                   PUSH(counter_stack,
  7240.                        sizeof (struct counter_frame));
  7241.                   cf = ((struct counter_frame *)
  7242.                     counter_stack->sp);
  7243.                   cf->tag = re_se_pushpos;
  7244.                   cf->val = urhere;
  7245.                   cf->inherited_from = 0;
  7246.                   cf->cdr = old_cf;
  7247.                   break;
  7248.                 }
  7249.                 
  7250.                   case re_se_chkpos:
  7251.                 {
  7252.                   int urhere =
  7253.                     ((int)(tst_pos - tst_str_half)
  7254.                      + ((tst_half == 0) ? 0 : size1));
  7255.                   struct counter_frame * cf
  7256.                     = ((struct counter_frame *)
  7257.                        counter_stack->sp);
  7258.                   if (cf->val == urhere)
  7259.                     goto test_do_return;
  7260.                   cf->val = urhere;
  7261.                   break;
  7262.                 }
  7263.                 break;
  7264.  
  7265.                   case re_se_poppos:
  7266.                 POP(counter_stack,
  7267.                     sizeof (struct counter_frame));
  7268.                 break;
  7269.                 
  7270.                 
  7271.                   CASE(re_se_at_dot,se_at_dot)
  7272.                   CASE(re_se_syntax,se_syntax)
  7273.                   CASE(re_se_not_syntax,se_not_syntax)
  7274. #ifdef emacs
  7275.                 this release lacks emacs support;
  7276.                 (coming soon);
  7277. #endif
  7278.                 break;
  7279.                   case re_se_lparen:
  7280.                   case re_se_rparen:
  7281.                   case re_se_backref:
  7282.                   case re_se_iter:
  7283.                   case re_se_end_iter:
  7284.                   case re_floogle_flap:
  7285.                 ret_val = 0;
  7286.                 goto test_do_return;
  7287.                   }
  7288.               }
  7289.             else
  7290.               {
  7291. #ifdef HAVE_GNUC_LABELS
  7292.                 goto *se_lables2[(rxb->se_params [effect].se)];
  7293. #endif
  7294. #ifdef RX_DEBUG
  7295.               if (rx_debug_trace)
  7296.                 fprintf (stderr, "....%d %d>> %s %d %d\n", line_no,
  7297.                      backtrack_depth,
  7298.                      efnames2[rxb->se_params [effect].se],
  7299.                      rxb->se_params [effect].op1,
  7300.                      rxb->se_params [effect].op2);
  7301. #endif
  7302.                 switch (rxb->se_params [effect].se)
  7303.                   {
  7304.                   CASE(re_se_lparen,se_lparen)
  7305.                 {
  7306.                   int urhere =
  7307.                     ((int)(tst_pos - tst_str_half)
  7308.                      + ((tst_half == 0) ? 0 : size1));
  7309.                   
  7310.                   int reg = rxb->se_params [effect].op1;
  7311. #if 0
  7312.                   if (reg > last_l)
  7313. #endif
  7314.                     {
  7315.                       lparen[reg] = urhere + 1;
  7316.                       /* In addition to making this assignment,
  7317.                        * we now know that lower numbered regs
  7318.                        * that haven't already been assigned,
  7319.                        * won't be.  We make sure they're
  7320.                        * filled with -1, so they can be
  7321.                        * recognized as unassigned.
  7322.                        */
  7323.                       if (last_l < reg)
  7324.                     while (++last_l < reg)
  7325.                       lparen[last_l] = -1;
  7326.                     }
  7327.                   break;
  7328.                 }
  7329.                 
  7330.                   CASE(re_se_rparen,se_rparen)
  7331.                 {
  7332.                   int urhere =
  7333.                     ((int)(tst_pos - tst_str_half)
  7334.                      + ((tst_half == 0) ? 0 : size1));
  7335.                   int reg = rxb->se_params [effect].op1;
  7336.                   rparen[reg] = urhere + 1;
  7337.                   if (last_r < reg)
  7338.                     {
  7339.                       while (++last_r < reg)
  7340.                     rparen[last_r] = -1;
  7341.                     }
  7342.                   break;
  7343.                 }
  7344.                 
  7345.                   CASE(re_se_backref,se_backref)
  7346.                 {
  7347.                   int reg = rxb->se_params [effect].op1;
  7348.                   if (reg > last_r || rparen[reg] < 0)
  7349.                     goto test_do_return;
  7350.                   {
  7351.                     /* fixme */
  7352.                     const unsigned char * there
  7353.                       = tst_str_half + lparen[reg];
  7354.                     const unsigned char * last
  7355.                       = tst_str_half + rparen[reg];
  7356.                     const unsigned char * here = tst_pos + 1;
  7357.  
  7358.                     if ((here == tst_end_half) && string2
  7359.                     && (tst_str_half
  7360.                         == (unsigned char *) string1)
  7361.                     && (tst_end_half != abs_end))
  7362.                       {
  7363.                     here = (unsigned char *)string2;
  7364.                     tst_end_half = abs_end;
  7365.                       }
  7366.                     
  7367.                     while (there < last && here < tst_end_half)    /* 4% */
  7368.                       if (TRANSLATE(*there) /* &&&& 6% */
  7369.                       != TRANSLATE(*here))
  7370.                     goto test_do_return;
  7371.                       else
  7372.                     {
  7373.                       ++there; ++here;
  7374.                       if ((here == tst_end_half) && string2
  7375.                           && (tst_str_half
  7376.                           == (unsigned char *)string1)
  7377.                           && (tst_end_half != abs_end))
  7378.                         {
  7379.                           here = (unsigned char *)string2;
  7380.                           tst_end_half = abs_end;
  7381.                           tst_half = 1;
  7382.                         }
  7383.                     }
  7384.                     if (there != last)
  7385.                       goto test_do_return;
  7386.                     tst_pos = here - 1;
  7387.                     if ((here == (unsigned char *)string2)
  7388.                     && (unsigned char *)string1)
  7389.                       {
  7390.                     tst_pos = ((unsigned char *)string1
  7391.                            + size1 - 1);
  7392.                     tst_end_half = tst_pos + 1;
  7393.                     tst_half = 0;
  7394.                       }
  7395.                   }
  7396.                   break;
  7397.                 }
  7398.                   CASE(re_se_iter,se_iter)
  7399.                 {
  7400.                   struct counter_frame * csp
  7401.                     = ((struct counter_frame *)
  7402.                        counter_stack->sp);
  7403.                   if (csp->val == rxb->se_params[effect].op2)
  7404.                     goto test_do_return;
  7405.                   else
  7406.                     ++csp->val;
  7407.                   break;
  7408.                 }
  7409.                   CASE(re_se_end_iter,se_end_iter)
  7410.                 {
  7411.                   struct counter_frame * csp
  7412.                     = ((struct counter_frame *)
  7413.                        counter_stack->sp);
  7414.                   if (csp->val < rxb->se_params[effect].op1)
  7415.                     goto test_do_return;
  7416.                   else
  7417.                     {
  7418.                       struct counter_frame * source = csp;
  7419.                       while (source->inherited_from)
  7420.                     source = source->inherited_from;
  7421.                       if (!source || !source->cdr)
  7422.                     {
  7423.                       POP(counter_stack,
  7424.                           sizeof(struct counter_frame));
  7425.                     }
  7426.                       else
  7427.                     {
  7428.                       source = source->cdr;
  7429.                       csp->val = source->val;
  7430.                       csp->tag = source->tag;
  7431.                       csp->cdr = 0;
  7432.                       csp->inherited_from = source;
  7433.                     }
  7434.                     }
  7435.                   break;
  7436.                 }
  7437.                   case re_se_try:
  7438.                   case re_se_pushback:
  7439.                   case re_se_push0:
  7440.                   case re_se_pushpos:
  7441.                   case re_se_chkpos:
  7442.                   case re_se_poppos:
  7443.                   case re_se_at_dot:
  7444.                   case re_se_syntax:
  7445.                   case re_se_not_syntax:
  7446.                   case re_se_begbuf:
  7447.                   case re_se_hat:
  7448.                   case re_se_wordbeg:
  7449.                   case re_se_wordbound:
  7450.                   case re_se_notwordbound:
  7451.                   case re_se_wordend:
  7452.                   case re_se_endbuf:
  7453.                   case re_se_dollar:
  7454.                   case re_se_fail:
  7455.                   case re_se_win:
  7456.                   case re_floogle_flap:
  7457.                 ret_val = 0;
  7458.                 goto test_do_return;
  7459.                   }
  7460.               }
  7461.             el = el->cdr;
  7462.               }
  7463.             /* Now the side effects are done,
  7464.              * so get the next instruction.
  7465.              * and move on.
  7466.              */
  7467.             ifr = &df->future_frame;
  7468.             goto restart;
  7469.           }
  7470.           
  7471.         CASE(rx_backtrack_point,backtrack_point)
  7472.           {
  7473.             /* A backtrack point indicates that we've reached a
  7474.              * non-determinism in the superstate NFA.  This is a
  7475.              * loop that exhaustively searches the possibilities.
  7476.              *
  7477.              * A backtracking strategy is used.  We keep track of what
  7478.              * registers are valid so we can erase side effects.
  7479.              *
  7480.              * First, make sure there is some stack space to hold 
  7481.              * our state.
  7482.              */
  7483.  
  7484.             struct backtrack_frame * bf;
  7485.  
  7486.             PUSH(backtrack_stack, backtrack_frame_bytes);
  7487. #ifdef RX_DEBUG
  7488.             ++backtrack_depth;
  7489. #endif
  7490.  
  7491.             bf = ((struct backtrack_frame *)
  7492.               backtrack_stack->sp);
  7493.             {
  7494.               bf->stk_super = super;
  7495.               /* We prevent the current superstate from being
  7496.                * deleted from the superstate cache.
  7497.                */
  7498.               rx_lock_superstate (&rxb->rx, super);
  7499.               bf->stk_tst_pos = tst_pos;
  7500. #ifdef RX_DEBUG
  7501.               bf->stk_line_no = line_no;
  7502. #endif
  7503.               bf->stk_tst_half = tst_half;
  7504.               bf->stk_c = c;
  7505.               bf->stk_tst_str_half = tst_str_half;
  7506.               bf->stk_tst_end_half = tst_end_half;
  7507.               bf->stk_last_l = last_l;
  7508.               bf->stk_last_r = last_r;
  7509.               bf->df = ((struct rx_super_edge *)ifr->data_2)->options;
  7510.               bf->counter_stack_sp = (counter_stack
  7511.                           ? counter_stack->sp
  7512.                           : 0);
  7513.               bf->stk_test_ret = test_ret;
  7514.               if (rxb->match_regs_on_stack)
  7515.             {
  7516.               int x;
  7517.               regoff_t * stk =
  7518.                 (regoff_t *)((char *)bf + sizeof (*bf));
  7519.               for (x = 0; x <= last_l; ++x)
  7520.                 stk[x] = lparen[x];
  7521.               stk += x;
  7522.               for (x = 0; x <= last_r; ++x)
  7523.                 stk[x] = rparen[x];
  7524.             }
  7525.  
  7526.             }
  7527.  
  7528.             /* Here is a while loop whose body is mainly a function
  7529.              * call and some code to handle a return from that
  7530.              * function.
  7531.              *
  7532.              * From here on for the rest of `case backtrack_point' it
  7533.              * is unsafe to assume that the variables saved on the
  7534.              * stack are valid -- so reread their values from the stack
  7535.              * as needed.
  7536.              *
  7537.              * This lets us re-use one generation fewer stack saves in
  7538.              * the call-graph of a search.
  7539.              */
  7540.             
  7541.           while_non_det_options:
  7542. #ifdef RX_DEBUG
  7543.             ++lines_found;
  7544.             if (rx_debug_trace)
  7545.               fprintf (stderr, "@@@ %d calls %d @@@\n",
  7546.                    line_no, lines_found);
  7547.             
  7548.             line_no = lines_found;
  7549. #endif
  7550.             
  7551.             if (!bf->df->next_same_super_edge)
  7552.               {
  7553.             /* This is a tail-call optimization -- we don't recurse
  7554.              * for the last of the possible futures.
  7555.              */
  7556.             ifr = (bf->df->effects
  7557.                    ? &bf->df->side_effects_frame
  7558.                    : &bf->df->future_frame);
  7559.  
  7560.             rx_unlock_superstate (&rxb->rx, super);
  7561.             POP(backtrack_stack, backtrack_frame_bytes);
  7562. #ifdef RX_DEBUG
  7563.             --backtrack_depth;
  7564. #endif
  7565.             goto restart;
  7566.               }
  7567.             else
  7568.               {
  7569.             if (counter_stack)
  7570.               {
  7571.                 struct counter_frame * old_cf
  7572.                   = ((struct counter_frame *)counter_stack->sp);
  7573.                 struct counter_frame * cf;
  7574.                 PUSH(counter_stack, sizeof (struct counter_frame));
  7575.                 cf = ((struct counter_frame *)counter_stack->sp);
  7576.                 cf->tag = old_cf->tag;
  7577.                 cf->val = old_cf->val;
  7578.                 cf->inherited_from = old_cf;
  7579.                 cf->cdr = 0;
  7580.               }            
  7581.             /* `Call' this test-match block */
  7582.             ifr = (bf->df->effects
  7583.                    ? &bf->df->side_effects_frame
  7584.                    : &bf->df->future_frame);
  7585.             goto recurse_test_match;
  7586.               }
  7587.  
  7588.             /* Returns in this block are accomplished by
  7589.              * goto test_do_return.  There are two cases.
  7590.              * If there is some search-stack left,
  7591.              * then it is a return from a `recursive' call.
  7592.              * If there is no search-stack left, then
  7593.              * we should return to the fastmap/search loop.
  7594.              */
  7595.             
  7596.           test_do_return:
  7597.  
  7598.             if (!backtrack_stack)
  7599.               {
  7600. #ifdef RX_DEBUG
  7601.             if (rx_debug_trace)
  7602.               fprintf (stderr, "!!! %d bails returning %d !!!\n",
  7603.                    line_no, test_ret);
  7604. #endif
  7605.  
  7606.             /* No more search-stack -- this test is done. */
  7607.             if (test_ret)
  7608.               goto return_from_test_match;
  7609.             else
  7610.               goto error_in_testing_match;
  7611.               }
  7612.  
  7613.             /* Ok..we're returning from a recursive call to 
  7614.              * the test match block:
  7615.              */
  7616.             
  7617. #ifdef RX_DEBUG
  7618.             if (rx_debug_trace)
  7619.               fprintf (stderr, "+++ %d returns %d (to %d)+++\n",
  7620.                    line_no, test_ret, bf->stk_line_no);
  7621. #endif
  7622.             bf = ((struct backtrack_frame *)
  7623.               backtrack_stack->sp);
  7624.  
  7625.             while (counter_stack
  7626.                && (!bf->counter_stack_sp
  7627.                    || (bf->counter_stack_sp != counter_stack->sp)))
  7628.               {
  7629.             POP(counter_stack, sizeof (struct counter_frame));
  7630.               }
  7631.  
  7632.             if (!test_ret)
  7633.               {
  7634.             POP (backtrack_stack, backtrack_frame_bytes);
  7635.             goto test_do_return;
  7636.               }
  7637.  
  7638.             /* If any possible future reaches the end of the 
  7639.              * string without failing, make sure we propogate 
  7640.              * that information to the caller.
  7641.              */
  7642.             if ((test_ret == -2) && first_found)
  7643.               {
  7644.             rx_unlock_superstate (&rxb->rx, bf->stk_super);
  7645.             POP (backtrack_stack, backtrack_frame_bytes);
  7646.             goto test_do_return;
  7647.               }
  7648.  
  7649.             if (bf->stk_test_ret < 0)
  7650.               test_ret = bf->stk_test_ret;
  7651.  
  7652.             last_l = bf->stk_last_l;
  7653.             last_r = bf->stk_last_r;
  7654.             bf->df = bf->df->next_same_super_edge;
  7655.             super = bf->stk_super;
  7656.             tst_pos = bf->stk_tst_pos;
  7657.             tst_half = bf->stk_tst_half;
  7658.             c = bf->stk_c;
  7659.             tst_str_half = bf->stk_tst_str_half;
  7660.             tst_end_half = bf->stk_tst_end_half;
  7661. #ifdef RX_DEBUG
  7662.             line_no = bf->stk_line_no;
  7663. #endif
  7664.  
  7665.             if (rxb->match_regs_on_stack)
  7666.               {
  7667.             int x;
  7668.             regoff_t * stk =
  7669.               (regoff_t *)((char *)bf + sizeof (*bf));
  7670.             for (x = 0; x <= last_l; ++x)
  7671.               lparen[x] = stk[x];
  7672.             stk += x;
  7673.             for (x = 0; x <= last_r; ++x)
  7674.               rparen[x] = stk[x];
  7675.               }
  7676.  
  7677.             goto while_non_det_options;
  7678.           }
  7679.  
  7680.           
  7681.         CASE(rx_cache_miss,cache_miss)
  7682.           /* Because the superstate NFA is lazily constructed,
  7683.            * and in fact may erode from underneath us, we sometimes
  7684.            * have to construct the next instruction from the hard way.
  7685.            * This invokes one step in the lazy-conversion.
  7686.            */
  7687.           ifr = rx_handle_cache_miss (&rxb->rx, super, c, ifr->data_2);
  7688.           if (!ifr)
  7689.             {
  7690.               test_ret = 0;
  7691.               goto test_do_return;
  7692.             }
  7693.           goto restart;
  7694.           
  7695.         CASE(rx_backtrack,backtrack)
  7696.           /* RX_BACKTRACK means that we've reached the empty
  7697.            * superstate, indicating that match can't succeed
  7698.            * from this point.
  7699.            */
  7700.           goto test_do_return;
  7701.         case rx_next_char:
  7702.         case rx_error_inx:
  7703.         case rx_num_instructions:
  7704.           ret_val = 0;
  7705.           goto test_do_return;
  7706.         }
  7707.         }
  7708.     }
  7709.  
  7710.  
  7711.     /* Healthy exists from the test-match loop do a 
  7712.      * `goto return_from_test_match'   On the other hand, 
  7713.      * we might end up here.
  7714.      */
  7715.       error_in_testing_match:
  7716.     ret_val = -2;
  7717.     goto finish;
  7718.  
  7719.  
  7720.     /***** fastmap/search loop body
  7721.      *          considering the results testing for a match
  7722.      */
  7723.  
  7724.       return_from_test_match:
  7725.  
  7726.     if (best_last_l >= 0)
  7727.       {
  7728.         if (regs && (regs->start != best_lparen))
  7729.           {
  7730.         bcopy (best_lparen, regs->start,
  7731.                regs->num_regs * sizeof (int));
  7732.         bcopy (best_rparen, regs->end,
  7733.                regs->num_regs * sizeof (int));
  7734.           }
  7735.         if (regs && !rxb->no_sub)
  7736.           {
  7737.         int q;
  7738.         int bound = (regs->num_regs > num_regs
  7739.                  ? regs->num_regs
  7740.                  : num_regs);
  7741.         regoff_t * s = regs->start;
  7742.         regoff_t * e = regs->end;
  7743.         for (q = best_last_l + 1;  q < bound; ++q)
  7744.           s[q] = e[q] = -1;
  7745.           }
  7746.         ret_val = best_lparen[0];
  7747.         goto finish;
  7748.       }
  7749.  
  7750.     /***** fastmap/search loop,  increment and loop-test */
  7751.  
  7752.     pos += search_direction;
  7753.     startpos += search_direction;
  7754.  
  7755.       } while (startpos < search_end);
  7756.  
  7757.  
  7758.   /**** Exit code for fastmap/searchloop and the entire re_search_2 fn. */
  7759.  
  7760.   finish:
  7761.     /* Unset the fastmap sentinel */
  7762.     if (fastmap_chr >= 0)
  7763.       fastmap[fastmap_chr] = fastmap_val;
  7764.  
  7765.     if (start_super)
  7766.       rx_unlock_superstate (&rxb->rx, start_super);
  7767.  
  7768. #ifdef REGEX_MALLOC
  7769.     if (lparen) free (lparen);
  7770.     if (rparen) free (rparen);
  7771.     if (best_lpspace) free (best_lpspace);
  7772.     if (best_rpspace) free (best_rpspace);
  7773. #else
  7774.     alloca (0);
  7775. #endif
  7776.     return ret_val;
  7777.   }
  7778. }
  7779.  
  7780. /* Like re_search_2, above, but only one string is specified, and
  7781.  * doesn't let you say where to stop matching.
  7782.  */
  7783.  
  7784. #ifdef __STDC__
  7785. int
  7786. re_search (struct re_pattern_buffer * rxb, const char *string,
  7787.        int size, int startpos, int range,
  7788.        struct re_registers *regs)
  7789. #else
  7790. int
  7791. re_search (rxb, string, size, startpos, range, regs)
  7792.      struct re_pattern_buffer * rxb;
  7793.      const char * string;
  7794.      int size;
  7795.      int startpos;
  7796.      int range;
  7797.      struct re_registers *regs;
  7798. #endif
  7799. {
  7800.   return re_search_2 (rxb, 0, 0, string, size, startpos, range, regs, size);
  7801. }
  7802.  
  7803. #ifdef __STDC__
  7804. int
  7805. re_match_2 (struct re_pattern_buffer * rxb,
  7806.         const char * string1, int size1,
  7807.         const char * string2, int size2,
  7808.         int pos, struct re_registers *regs, int stop)
  7809. #else
  7810. int
  7811. re_match_2 (rxb, string1, size1, string2, size2, pos, regs, stop)
  7812.      struct re_pattern_buffer * rxb;
  7813.      const char * string1;
  7814.      int size1;
  7815.      const char * string2;
  7816.      int size2;
  7817.      int pos;
  7818.      struct re_registers *regs;
  7819.      int stop;
  7820. #endif
  7821. {
  7822.   struct re_registers some_regs;
  7823.   regoff_t start;
  7824.   regoff_t end;
  7825.   int srch;
  7826.   int save = rxb->regs_allocated;
  7827.   struct re_registers * regs_to_pass = regs;
  7828.  
  7829.   if (!regs)
  7830.     {
  7831.       some_regs.start = &start;
  7832.       some_regs.end = &end;
  7833.       some_regs.num_regs = 1;
  7834.       regs_to_pass = &some_regs;
  7835.       rxb->regs_allocated = REGS_FIXED;
  7836.     }
  7837.  
  7838.   srch = re_search_2 (rxb, string1, size1, string2, size2,
  7839.               pos, 1, regs_to_pass, stop);
  7840.   if (regs_to_pass != regs)
  7841.     rxb->regs_allocated = save;
  7842.   if (srch < 0)
  7843.     return srch;
  7844.   return regs_to_pass->end[0] - regs_to_pass->start[0];
  7845. }
  7846.  
  7847. /* re_match is like re_match_2 except it takes only a single string.  */
  7848.  
  7849. #ifdef __STDC__
  7850. int
  7851. re_match (struct re_pattern_buffer * rxb,
  7852.       const char * string,
  7853.       int size, int pos,
  7854.       struct re_registers *regs)
  7855. #else
  7856. int
  7857. re_match (rxb, string, size, pos, regs)
  7858.      struct re_pattern_buffer * rxb;
  7859.      const char *string;
  7860.      int size;
  7861.      int pos;
  7862.      struct re_registers *regs;
  7863. #endif
  7864. {
  7865.   return re_match_2 (rxb, string, size, 0, 0, pos, regs, size);
  7866. }
  7867.  
  7868.  
  7869.  
  7870. /* Set by `re_set_syntax' to the current regexp syntax to recognize.  Can
  7871.    also be assigned to arbitrarily: each pattern buffer stores its own
  7872.    syntax, so it can be changed between regex compilations.  */
  7873. reg_syntax_t re_syntax_options = RE_SYNTAX_EMACS;
  7874.  
  7875.  
  7876. /* Specify the precise syntax of regexps for compilation.  This provides
  7877.    for compatibility for various utilities which historically have
  7878.    different, incompatible syntaxes.
  7879.  
  7880.    The argument SYNTAX is a bit mask comprised of the various bits
  7881.    defined in regex.h.  We return the old syntax.  */
  7882.  
  7883. #ifdef __STDC__
  7884. reg_syntax_t
  7885. re_set_syntax (reg_syntax_t syntax)
  7886. #else
  7887. reg_syntax_t
  7888. re_set_syntax (syntax)
  7889.     reg_syntax_t syntax;
  7890. #endif
  7891. {
  7892.   reg_syntax_t ret = re_syntax_options;
  7893.  
  7894.   re_syntax_options = syntax;
  7895.   return ret;
  7896. }
  7897.  
  7898.  
  7899. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  7900.    ENDS.  Subsequent matches using PATTERN_BUFFER and REGS will use
  7901.    this memory for recording register information.  STARTS and ENDS
  7902.    must be allocated using the malloc library routine, and must each
  7903.    be at least NUM_REGS * sizeof (regoff_t) bytes long.
  7904.  
  7905.    If NUM_REGS == 0, then subsequent matches should allocate their own
  7906.    register data.
  7907.  
  7908.    Unless this function is called, the first search or match using
  7909.    PATTERN_BUFFER will allocate its own register data, without
  7910.    freeing the old data.  */
  7911.  
  7912. #ifdef __STDC__
  7913. void
  7914. re_set_registers (struct re_pattern_buffer *bufp,
  7915.           struct re_registers *regs,
  7916.           unsigned num_regs,
  7917.           regoff_t * starts, regoff_t * ends)
  7918. #else
  7919. void
  7920. re_set_registers (bufp, regs, num_regs, starts, ends)
  7921.      struct re_pattern_buffer *bufp;
  7922.      struct re_registers *regs;
  7923.      unsigned num_regs;
  7924.      regoff_t * starts;
  7925.      regoff_t * ends;
  7926. #endif
  7927. {
  7928.   if (num_regs)
  7929.     {
  7930.       bufp->regs_allocated = REGS_REALLOCATE;
  7931.       regs->num_regs = num_regs;
  7932.       regs->start = starts;
  7933.       regs->end = ends;
  7934.     }
  7935.   else
  7936.     {
  7937.       bufp->regs_allocated = REGS_UNALLOCATED;
  7938.       regs->num_regs = 0;
  7939.       regs->start = regs->end = (regoff_t) 0;
  7940.     }
  7941. }
  7942.  
  7943.  
  7944.  
  7945. /* re_compile_pattern is the GNU regular expression compiler: it
  7946.    compiles PATTERN (of length SIZE) and puts the result in RXB.
  7947.    Returns 0 if the pattern was valid, otherwise an error string.
  7948.  
  7949.    Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  7950.    are set in RXB on entry.
  7951.  
  7952.    We call rx_compile to do the actual compilation.  */
  7953.  
  7954. #ifdef __STDC__
  7955. const char *
  7956. re_compile_pattern (const char *pattern,
  7957.             int length,
  7958.             struct re_pattern_buffer * rxb)
  7959. #else
  7960. const char *
  7961. re_compile_pattern (pattern, length, rxb)
  7962.      const char *pattern;
  7963.      int length;
  7964.      struct re_pattern_buffer * rxb;
  7965. #endif
  7966. {
  7967.   reg_errcode_t ret;
  7968.  
  7969.   /* GNU code is written to assume at least RE_NREGS registers will be set
  7970.      (and at least one extra will be -1).  */
  7971.   rxb->regs_allocated = REGS_UNALLOCATED;
  7972.  
  7973.   /* And GNU code determines whether or not to get register information
  7974.      by passing null for the REGS argument to re_match, etc., not by
  7975.      setting no_sub.  */
  7976.   rxb->no_sub = 0;
  7977.  
  7978.   rxb->rx.local_cset_size = 256;
  7979.  
  7980.   /* Match anchors at newline.  */
  7981.   rxb->newline_anchor = 1;
  7982.  
  7983.   rxb->re_nsub = 0;
  7984.   rxb->start = 0;
  7985.   rxb->se_params = 0;
  7986.   rxb->rx.nodec = 0;
  7987.   rxb->rx.epsnodec = 0;
  7988.   rxb->rx.instruction_table = 0;
  7989.   rxb->rx.nfa_states = 0;
  7990.   rxb->rx.start = 0;
  7991.   
  7992.   ret = rx_compile (pattern, length, re_syntax_options, rxb);
  7993.  
  7994.   return rx_error_msg[(int) ret];
  7995. }
  7996.  
  7997.  
  7998.  
  7999. #ifdef __STDC__
  8000. int
  8001. re_compile_fastmap (struct re_pattern_buffer * rxb)
  8002. #else
  8003. int
  8004. re_compile_fastmap (rxb)
  8005.      struct re_pattern_buffer * rxb;
  8006. #endif
  8007. {
  8008.   rx_blow_up_fastmap (rxb);
  8009.   return 0;
  8010. }
  8011.  
  8012.  
  8013.  
  8014.  
  8015. /* Entry points compatible with 4.2 BSD regex library.  We don't define
  8016.    them if this is an Emacs or POSIX compilation.  */
  8017.  
  8018. #if !defined (emacs) && !defined (_POSIX_SOURCE) && 0
  8019.  
  8020. /* BSD has one and only one pattern buffer.  */
  8021. static struct re_pattern_buffer rx_comp_buf;
  8022.  
  8023. #ifdef __STDC__
  8024. char *
  8025. re_comp (const char *s)
  8026. #else
  8027. char *
  8028. re_comp (s)
  8029.     const char *s;
  8030. #endif
  8031. {
  8032.   reg_errcode_t ret;
  8033.  
  8034.   if (!s)
  8035.     {
  8036.       if (!rx_comp_buf.buffer)
  8037.     return "No previous regular expression";
  8038.       return 0;
  8039.     }
  8040.  
  8041.   if (!rx_comp_buf.fastmap)
  8042.     {
  8043.       rx_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  8044.       if (!rx_comp_buf.fastmap)
  8045.     return "Memory exhausted";
  8046.     }
  8047.  
  8048.   /* Since `rx_exec' always passes NULL for the `regs' argument, we
  8049.      don't need to initialize the pattern buffer fields which affect it.  */
  8050.  
  8051.   /* Match anchors at newlines.  */
  8052.   rx_comp_buf.newline_anchor = 1;
  8053.  
  8054.   rx_comp_buf.re_nsub = 0;
  8055.   rx_comp_buf.start = 0;
  8056.   rx_comp_buf.se_params = 0;
  8057.   rx_comp_buf.rx.nodec = 0;
  8058.   rx_comp_buf.rx.epsnodec = 0;
  8059.   rx_comp_buf.rx.instruction_table = 0;
  8060.   rx_comp_buf.rx.nfa_states = 0;
  8061.   rx_comp_buf.rx.start = 0;
  8062.  
  8063.   ret = rx_compile (s, strlen (s), rx_syntax_options, &rx_comp_buf);
  8064.  
  8065.   /* Yes, we're discarding `const' here.  */
  8066.   return (char *) rx_error_msg[(int) ret];
  8067. }
  8068.  
  8069.  
  8070. #ifdef __STDC__
  8071. int
  8072. rx_exec (const char *s)
  8073. #else
  8074. int
  8075. rx_exec (s)
  8076.     const char *s;
  8077. #endif
  8078. {
  8079.   const int len = strlen (s);
  8080.   return
  8081.     0 <= re_search (&rx_comp_buf, s, len, 0, len, (struct rx_registers *) 0);
  8082. }
  8083. #endif /* not emacs and not _POSIX_SOURCE */
  8084.  
  8085.  
  8086.  
  8087. /* POSIX.2 functions.  Don't define these for Emacs.  */
  8088.  
  8089. #if !defined(emacs)
  8090.  
  8091. /* regcomp takes a regular expression as a string and compiles it.
  8092.  
  8093.    PREG is a regex_t *.  We do not expect any fields to be initialized,
  8094.    since POSIX says we shouldn't.  Thus, we set
  8095.  
  8096.      `buffer' to the compiled pattern;
  8097.      `used' to the length of the compiled pattern;
  8098.      `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  8099.        REG_EXTENDED bit in CFLAGS is set; otherwise, to
  8100.        RE_SYNTAX_POSIX_BASIC;
  8101.      `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  8102.      `fastmap' and `fastmap_accurate' to zero;
  8103.      `re_nsub' to the number of subexpressions in PATTERN.
  8104.  
  8105.    PATTERN is the address of the pattern string.
  8106.  
  8107.    CFLAGS is a series of bits which affect compilation.
  8108.  
  8109.      If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  8110.      use POSIX basic syntax.
  8111.  
  8112.      If REG_NEWLINE is set, then . and [^...] don't match newline.
  8113.      Also, regexec will try a match beginning after every newline.
  8114.  
  8115.      If REG_ICASE is set, then we considers upper- and lowercase
  8116.      versions of letters to be equivalent when matching.
  8117.  
  8118.      If REG_NOSUB is set, then when PREG is passed to regexec, that
  8119.      routine will report only success or failure, and nothing about the
  8120.      registers.
  8121.  
  8122.    It returns 0 if it succeeds, nonzero if it doesn't.  (See regex.h for
  8123.    the return codes and their meanings.)  */
  8124.  
  8125.  
  8126. #ifdef __STDC__
  8127. int
  8128. regcomp (regex_t * preg, const char * pattern, int cflags)
  8129. #else
  8130. int
  8131. regcomp (preg, pattern, cflags)
  8132.     regex_t * preg;
  8133.     const char * pattern;
  8134.     int cflags;
  8135. #endif
  8136. {
  8137.   reg_errcode_t ret;
  8138.   unsigned syntax
  8139.     = cflags & REG_EXTENDED ? RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
  8140.  
  8141.   /* regex_compile will allocate the space for the compiled pattern.  */
  8142.   preg->buffer = 0;
  8143.   preg->allocated = 0;
  8144.  
  8145.   preg->fastmap = malloc (256);
  8146.   if (!preg->fastmap)
  8147.     return REG_ESPACE;
  8148.   preg->fastmap_accurate = 0;
  8149.  
  8150.   if (cflags & REG_ICASE)
  8151.     {
  8152.       unsigned i;
  8153.  
  8154.       preg->translate = (char *) malloc (256);
  8155.       if (!preg->translate)
  8156.         return (int) REG_ESPACE;
  8157.  
  8158.       /* Map uppercase characters to corresponding lowercase ones.  */
  8159.       for (i = 0; i < CHAR_SET_SIZE; i++)
  8160.         preg->translate[i] = isupper (i) ? tolower (i) : i;
  8161.     }
  8162.   else
  8163.     preg->translate = 0;
  8164.  
  8165.   /* If REG_NEWLINE is set, newlines are treated differently.  */
  8166.   if (cflags & REG_NEWLINE)
  8167.     { /* REG_NEWLINE implies neither . nor [^...] match newline.  */
  8168.       syntax &= ~RE_DOT_NEWLINE;
  8169.       syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  8170.       /* It also changes the matching behavior.  */
  8171.       preg->newline_anchor = 1;
  8172.     }
  8173.   else
  8174.     preg->newline_anchor = 0;
  8175.  
  8176.   preg->no_sub = !!(cflags & REG_NOSUB);
  8177.  
  8178.   /* POSIX says a null character in the pattern terminates it, so we
  8179.      can use strlen here in compiling the pattern.  */
  8180.   preg->re_nsub = 0;
  8181.   preg->start = 0;
  8182.   preg->se_params = 0;
  8183.   preg->rx.nodec = 0;
  8184.   preg->rx.epsnodec = 0;
  8185.   preg->rx.instruction_table = 0;
  8186.   preg->rx.nfa_states = 0;
  8187.   preg->rx.start = 0;
  8188.   preg->rx.local_cset_size = 256;
  8189.   ret = rx_compile (pattern, strlen (pattern), syntax, preg);
  8190.  
  8191.   /* POSIX doesn't distinguish between an unmatched open-group and an
  8192.      unmatched close-group: both are REG_EPAREN.  */
  8193.   if (ret == REG_ERPAREN) ret = REG_EPAREN;
  8194.  
  8195.   return (int) ret;
  8196. }
  8197.  
  8198.  
  8199. /* regexec searches for a given pattern, specified by PREG, in the
  8200.    string STRING.
  8201.  
  8202.    If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  8203.    `regcomp', we ignore PMATCH.  Otherwise, we assume PMATCH has at
  8204.    least NMATCH elements, and we set them to the offsets of the
  8205.    corresponding matched substrings.
  8206.  
  8207.    EFLAGS specifies `execution flags' which affect matching: if
  8208.    REG_NOTBOL is set, then ^ does not match at the beginning of the
  8209.    string; if REG_NOTEOL is set, then $ does not match at the end.
  8210.  
  8211.    We return 0 if we find a match and REG_NOMATCH if not.  */
  8212.  
  8213. #ifdef __STDC__
  8214. int
  8215. regexec (const regex_t *preg, const char *string,
  8216.      size_t nmatch, regmatch_t pmatch[],
  8217.      int eflags)
  8218. #else
  8219. int
  8220. regexec (preg, string, nmatch, pmatch, eflags)
  8221.     const regex_t *preg;
  8222.     const char *string;
  8223.     size_t nmatch;
  8224.     regmatch_t pmatch[];
  8225.     int eflags;
  8226. #endif
  8227. {
  8228.   int ret;
  8229.   struct re_registers regs;
  8230.   regex_t private_preg;
  8231.   int len = strlen (string);
  8232.   boolean want_reg_info = !preg->no_sub && nmatch > 0;
  8233.  
  8234.   private_preg = *preg;
  8235.  
  8236.   private_preg.not_bol = !!(eflags & REG_NOTBOL);
  8237.   private_preg.not_eol = !!(eflags & REG_NOTEOL);
  8238.  
  8239.   /* The user has told us exactly how many registers to return
  8240.    * information about, via `nmatch'.  We have to pass that on to the
  8241.    * matching routines.
  8242.    */
  8243.   private_preg.regs_allocated = REGS_FIXED;
  8244.  
  8245.   if (want_reg_info)
  8246.     {
  8247.       regs.num_regs = nmatch;
  8248.       regs.start = TALLOC (nmatch, regoff_t);
  8249.       regs.end = TALLOC (nmatch, regoff_t);
  8250.       if (regs.start == 0 || regs.end == 0)
  8251.         return (int) REG_NOMATCH;
  8252.     }
  8253.  
  8254.   /* Perform the searching operation.  */
  8255.   ret = re_search (&private_preg,
  8256.            string, len,
  8257.                    /* start: */ 0,
  8258.            /* range: */ len,
  8259.                    want_reg_info ? ®s : (struct re_registers *) 0);
  8260.  
  8261.   /* Copy the register information to the POSIX structure.  */
  8262.   if (want_reg_info)
  8263.     {
  8264.       if (ret >= 0)
  8265.         {
  8266.           unsigned r;
  8267.  
  8268.           for (r = 0; r < nmatch; r++)
  8269.             {
  8270.               pmatch[r].rm_so = regs.start[r];
  8271.               pmatch[r].rm_eo = regs.end[r];
  8272.             }
  8273.         }
  8274.  
  8275.       /* If we needed the temporary register info, free the space now.  */
  8276.       free (regs.start);
  8277.       free (regs.end);
  8278.     }
  8279.  
  8280.   /* We want zero return to mean success, unlike `re_search'.  */
  8281.   return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  8282. }
  8283.  
  8284.  
  8285. /* Returns a message corresponding to an error code, ERRCODE, returned
  8286.    from either regcomp or regexec.   */
  8287.  
  8288. #ifdef __STDC__
  8289. size_t
  8290. regerror (int errcode, const regex_t *preg,
  8291.       char *errbuf, size_t errbuf_size)
  8292. #else
  8293. size_t
  8294. regerror (errcode, preg, errbuf, errbuf_size)
  8295.     int errcode;
  8296.     const regex_t *preg;
  8297.     char *errbuf;
  8298.     size_t errbuf_size;
  8299. #endif
  8300. {
  8301.   const char *msg
  8302.     = rx_error_msg[errcode] == 0 ? "Success" : rx_error_msg[errcode];
  8303.   size_t msg_size = strlen (msg) + 1; /* Includes the 0.  */
  8304.  
  8305.   if (errbuf_size != 0)
  8306.     {
  8307.       if (msg_size > errbuf_size)
  8308.         {
  8309.           strncpy (errbuf, msg, errbuf_size - 1);
  8310.           errbuf[errbuf_size - 1] = 0;
  8311.         }
  8312.       else
  8313.         strcpy (errbuf, msg);
  8314.     }
  8315.  
  8316.   return msg_size;
  8317. }
  8318.  
  8319.  
  8320. /* Free dynamically allocated space used by PREG.  */
  8321.  
  8322. #ifdef __STDC__
  8323. void
  8324. regfree (regex_t *preg)
  8325. #else
  8326. void
  8327. regfree (preg)
  8328.     regex_t *preg;
  8329. #endif
  8330. {
  8331.   if (preg->buffer != 0)
  8332.     free (preg->buffer);
  8333.   preg->buffer = 0;
  8334.   preg->allocated = 0;
  8335.  
  8336.   if (preg->fastmap != 0)
  8337.     free (preg->fastmap);
  8338.   preg->fastmap = 0;
  8339.   preg->fastmap_accurate = 0;
  8340.  
  8341.   if (preg->translate != 0)
  8342.     free (preg->translate);
  8343.   preg->translate = 0;
  8344. }
  8345.  
  8346. #endif /* not emacs  */
  8347.  
  8348.  
  8349.  
  8350.  
  8351.  
  8352.